Ruby on Rails
Tuesday, June 5, 2012
I'm a bit unclear what's going on with relations. I'm hoping someone can help explain it to me.
Lets say we have a model with orders and line_items such that an order has many line_items. Thus:
class Order
has_may :line_items
end
class LineItem
belongs_to :order
end
When I have an order and call line_items what's going on? e.g.
Order order = Order.first
order.line_items.each {|li| puts li }
I thought that was basically an alias for:
Order order = Order.first
LineItem.where(:order_id => order.id).each {|li| puts li }
but, this isn't consistent with batching.
So if we do
Order order = Order.first
# this doesn't appear to work, it's not batching, just finding all of the line items and iterating
order.line_items.find_each {|li| puts li }
# this seems to work as expected (i.e. it batches the retrieval from the db)
LineItem.where(:order_id => order.id).find_each {|li| puts li }
Can anyone explain to me why these to are different?
-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/kb5hRwQtHvwJ.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment