eager load bug?

o = Organization.first o.people.includes(:addresses).each do |p| puts p puts p.addresses end # 2 queries are executed, normal and for eager loading addresses

but

o.people.loaded? => false

Is that a bug or by-design behavior that o.people.includes(:address) won't store the fetched records in o.people association ?

Robert Pankowecki

That's by design. When you chain a scope off an associationproxy, it's basically syntactic sugar for constructing your query the long way, by doing Person.where(:organization_id => o.id)[...]

In the case you gave, above, the people association never needed to load as the new scope was created, and to store the result of the query you eventually executed in that association would be the wrong thing to do -- it may have had additional conditions limiting the results that would not fully load the association.

Inside your block, though, p.addresses.loaded? will be true, which is what you asked for.

Hope this helps!