Skip to content

Commit

Permalink
reintroduce patch from #726 to handle nested eager loading via associ…
Browse files Browse the repository at this point in the history
…ations
  • Loading branch information
jaylevitt committed Nov 30, 2011
1 parent cbeeaa6 commit 24b8814
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def construct_association(record, join_part, row)

macro = join_part.reflection.macro
if macro == :has_one
return if record.association_cache.key?(join_part.reflection.name)
return record.association(join_part.reflection.name).target if record.association_cache.key?(join_part.reflection.name)
association = join_part.instantiate(row) unless row[join_part.aliased_primary_key].nil?
set_target_and_inverse(join_part, association, record)
else
Expand Down
35 changes: 35 additions & 0 deletions activerecord/test/cases/associations/eager_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,41 @@ def test_nested_loading_with_no_associations
end
end

def test_nested_loading_through_has_one_association
aa = AuthorAddress.find(author_addresses(:david_address).id, :include => {:author => :posts})
assert_equal aa.author.posts.count, aa.author.posts.length
end

def test_nested_loading_through_has_one_association_with_order
aa = AuthorAddress.find(author_addresses(:david_address).id, :include => {:author => :posts}, :order => 'author_addresses.id')
assert_equal aa.author.posts.count, aa.author.posts.length
end

def test_nested_loading_through_has_one_association_with_order_on_association
aa = AuthorAddress.find(author_addresses(:david_address).id, :include => {:author => :posts}, :order => 'authors.id')
assert_equal aa.author.posts.count, aa.author.posts.length
end

def test_nested_loading_through_has_one_association_with_order_on_nested_association
aa = AuthorAddress.find(author_addresses(:david_address).id, :include => {:author => :posts}, :order => 'posts.id')
assert_equal aa.author.posts.count, aa.author.posts.length
end

def test_nested_loading_through_has_one_association_with_conditions
aa = AuthorAddress.find(author_addresses(:david_address).id, :include => {:author => :posts}, :conditions => "author_addresses.id > 0")
assert_equal aa.author.posts.count, aa.author.posts.length
end

def test_nested_loading_through_has_one_association_with_conditions_on_association
aa = AuthorAddress.find(author_addresses(:david_address).id, :include => {:author => :posts}, :conditions => "authors.id > 0")
assert_equal aa.author.posts.count, aa.author.posts.length
end

def test_nested_loading_through_has_one_association_with_conditions_on_nested_association
aa = AuthorAddress.find(author_addresses(:david_address).id, :include => {:author => :posts}, :conditions => "posts.id > 0")
assert_equal aa.author.posts.count, aa.author.posts.length
end

def test_eager_association_loading_with_belongs_to_and_foreign_keys
pets = Pet.find(:all, :include => :owner)
assert_equal 3, pets.length
Expand Down

0 comments on commit 24b8814

Please sign in to comment.