Forum: Ruby on Rails Duplicate queries while eager loading multiple associations

Posted by Dennis Krupenik (Guest)
on 2012-09-17 09:08
(Received via mailing list)
Hello all,

given the following models and console log: 
https://gist.github.com/3735116

I do not understand why the latter statements (o.order_items and 
o.total)
yield sql queries.
Have I missed something in AR documentation? What should be done to get 
rid
of redundant sql queries?
Posted by Frederick Cheung (Guest)
on 2012-09-17 09:14
(Received via mailing list)
On Monday, September 17, 2012 2:45:05 AM UTC+1, Dennis Krupenik wrote:
>
> oi = o.order_items.includes(item: :photos)

does not mutate o.order_items, adding the includes to it: it returns a 
new
array of objects that has the extra options applied.

You do o = Order.includes(order_items: {item: photos}).last in which 
case

o.order_items should  not execute any queries

Fred
Posted by Dennis Krupenik (Guest)
on 2012-09-17 16:19
(Received via mailing list)
thanks a lot, that did it
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.