From 3faab42bc042d0c8a91a922812178940b5c85ab7 Mon Sep 17 00:00:00 2001 From: Akshay Rawat Date: Sun, 29 Mar 2009 13:01:31 -0400 Subject: [PATCH] Bug 2341 fixed Allow eager loading of has_many association in a has_one association --- activerecord/lib/active_record/associations.rb | 2 +- activerecord/test/cases/associations/eager_test.rb | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 6d25b36..559f57a 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1890,7 +1890,7 @@ module ActiveRecord collection.target.push(association) 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? record.send("set_#{join.reflection.name}_target", association) when :belongs_to diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 4072381..0f421d2 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -38,7 +38,14 @@ class EagerAssociationTest < ActiveRecord::TestCase post = posts.find { |p| p.id == 1 } assert_equal Post.find(1).last_comment, post.last_comment end - + + def test_loading_has_one_with_has_many_using_non_preload + author_address = author_addresses(:david_address) + loaded_version = AuthorAddress.find(author_address.id, :include => {:author => :posts}, :order => 'posts.id') + assert_equal author_address, loaded_version + assert_equal 5, loaded_version.author.posts.size + end + def test_loading_with_one_association_with_non_preload posts = Post.find(:all, :include => :last_comment, :order => 'comments.id DESC') post = posts.find { |p| p.id == 1 } -- 1.6.0.1