rails/rails — pull request #3820 by jaylevitt (2011-12-01 00:18:47+00:00)
Title: reintroduce patch from #726 to handle nested eager loading via associations

This got missed in the lighthouse -> github transition..

Eager loading a nested has_many association with conditions or order on the nested table loads only the first record.

class AuthorAddress < ActiveRecord::Base
has_one :author
end

class Author < ActiveRecord::Base
has_many :posts
belongs_to :author_address
end

class Post < ActiveRecord::Base
belongs_to :author
end

author_address = AuthorAddress.find(1, :include => {:author => :posts}, :order => "authors.id")

Using an order or conditions clause on a different table than the base one uses legacy SQL (LEFT OUTER JOIN) to load, but it incorrectly (eagerly) loads posts. Only one, of the many posts, is eagerly loaded and unfortunately the association thinks it is fully loaded.

Therefore doing something like this:

author_address.author.posts.length

returns a different answer than this:

author_address.author.posts.count

(which of course hits the database again to get the correct answer)