On 8 June 2012 16:44, Max Reznichenko <max.rezniche...@gmail.com> wrote:
>> order.line_items != LineItem.where(:order_id => order.id)
>
> Let me rephrase Robert's words.
> The key difference here is, that LineItem.where(:order_id => order.id)
> doesn't fire the sql and returns ActiveRecord::Relation object.
> While the order.line_items runs SQL and returns the Array of records.
> The purpose of it is that u can change the resulting SQL and do the
> method chaining.
>
> Try next:
> line_items_by_order_id = LineItem.where(:order_id => order.id) # =>
> object of ActiveRecord::Relation class
> ordered_line_items_by_order_id = LineItem.where(:order_id =>
> order.id).order(:updated_at) # => object of ActiveRecord::Relation
> class
> ordered_line_items_by_order_id_with_positive_price =
> LineItem.where(:order_id =>
> order.id).order(:updated_at).where("line_items.price > ?", 20) # =>
> object of ActiveRecord::Relation class
> # ... and so on
> # where
> order.line_items # returns [LineItem, LineItem ...] array

It is noteworthy that one can still do
order.line_items.where( some condition )
even though order.line_items appears to be an Array.

Colin

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to