On Wednesday, November 20, 2019 at 1:21:41 PM UTC-8, Mason Kimble wrote:
>
> Basically, can I filter a dataset after it goes to the database?
>
> order_items = OrderItem.dataset.where(order_id: orderId).all (I know this 
> would query database)
>
> orders_items_to_match.each { | match_item |
>     matching_item = order_items.where(item_id: 
> match_item['orderItemId']).first  (Does this go back to database or in 
> memory)
>

This doesn't work.  order_items is an array, not a dataset.  

}
>
> 1. Is order_items an array of OrderItem models or a dataset?
>

An array.  If you want a dataset, leave off the .all 
 

> 2. If dataset, would .first execute query in memory to match item_id or 
> would it go back to the database
>

Datasets always query the Database when asked for results.
 

> 3. Is there another way to do this?  Basically I want to load all the 
> Children for a Parent from the database and then filter the Children in 
> memory and return a Model if a match is found.
>

You could probably use `order_items.find{|item| ...}`  for what you are 
describing.  This will return the first matching OrderItem if the block 
ever returns true, and nil if the block never returns true.

This is kind of a smell though, and should definitely be avoided if 
order_items can be a large array. You should see if it is possible to do 
the query you want in the database in a single query using a join or 
subquery.

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/2126f06a-2e63-48fd-b2f3-5c37848b195c%40googlegroups.com.

Reply via email to