I think that's what's always tripped me up. Most ActiveRecord commands still work normally on the array. I've always chained them and thought I was building a relation, not getting the array. It wasn't until I had a big set of records and discovered I couldn't use the association in a find_each that I tripped over this.
Thanks for the help. On Friday, June 8, 2012 9:02:45 AM UTC-7, Colin Law wrote: > > On 8 June 2012 16:44, Max Reznichenko > >> 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 > On Friday, June 8, 2012 9:02:45 AM UTC-7, Colin Law wrote: > > On 8 June 2012 16:44, Max Reznichenko > >> 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 view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/aIxiclDbJN8J. 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.