From 7589594dc8f659d79a87019ba40176d696f4f860 Mon Sep 17 00:00:00 2001 From: Fotos Georgiadis Date: Sat, 22 Jan 2011 00:16:25 +0200 Subject: [PATCH] Fixed bug with nested has_many eager loaded associations. --- activerecord/lib/active_record/associations.rb | 2 +- activerecord/test/cases/associations/eager_test.rb | 35 ++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 3a32581..dced226 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1967,7 +1967,7 @@ module ActiveRecord collection.__send__(:set_inverse_instance, association, record) when :has_one return if record.id.to_s != join.parent.record_id(row).to_s - return if record.instance_variable_defined?("@#{join.reflection.name}") + return record.instance_variable_get("@#{join.reflection.name}") if record.instance_variable_defined?("@#{join.reflection.name}") association = join.instantiate(row) unless row[join.aliased_primary_key].nil? set_target_and_inverse(join, association, record) when :belongs_to diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index b711719..ad454a7 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -79,6 +79,41 @@ class EagerAssociationTest < ActiveRecord::TestCase assert posts.first.comments.include?(comments(:greetings)) end + def test_loading_with_includes_on_has_one_association + aa = AuthorAddress.find(1, :include => {:author => :posts}) + assert_equal aa.author.posts.count, aa.author.posts.length + end + + def test_loading_with_includes_on_has_one_association_and_order + aa = AuthorAddress.find(1, :include => {:author => :posts}, :order => "author_addresses.id") + assert_equal aa.author.posts.count, aa.author.posts.length + end + + def test_loading_with_includes_on_has_one_association_and_order_in_first_associated_table + aa = AuthorAddress.find(1, :include => {:author => :posts}, :order => "authors.id") + assert_equal aa.author.posts.count, aa.author.posts.length + end + + def test_loading_with_includes_on_has_one_association_and_order_in_second_associated_table + aa = AuthorAddress.find(1, :include => {:author => :posts}, :order => "posts.id") + assert_equal aa.author.posts.count, aa.author.posts.length + end + + def test_loading_with_includes_on_has_one_association_and_conditions + aa = AuthorAddress.find(1, :include => {:author => :posts}, :conditions => "author_addresses.id > 0") + assert_equal aa.author.posts.count, aa.author.posts.length + end + + def test_loading_with_includes_on_has_one_association_and_conditions_in_first_associated_table + aa = AuthorAddress.find(1, :include => {:author => :posts}, :conditions => "authors.id > 0") + assert_equal aa.author.posts.count, aa.author.posts.length + end + + def test_loading_with_includes_on_has_one_association_and_conditions_in_second_associated_table + aa = AuthorAddress.find(1, :include => {:author => :posts}, :conditions => "posts.id > 0") + assert_equal aa.author.posts.count, aa.author.posts.length + end + def test_duplicate_middle_objects comments = Comment.find :all, :conditions => 'post_id = 1', :include => [:post => :author] assert_no_queries do -- 1.7.3.1