From 664b90abb4a4e5a4bf552915ed522de4695520bd Mon Sep 17 00:00:00 2001 From: Ernie Miller Date: Sat, 22 May 2010 15:31:06 -0400 Subject: [PATCH] Fix multiple self-referencing eager loads failing to join multiple times --- activerecord/lib/active_record/associations.rb | 4 ++-- .../associations/cascaded_eager_loading_test.rb | 10 +++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 5b0ba86..00c9128 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1901,7 +1901,7 @@ module ActiveRecord end def ==(other) - other.is_a?(JoinBase) && + other.class == self.class && other.active_record == active_record && other.table_joins == table_joins end @@ -1972,7 +1972,7 @@ module ActiveRecord end def ==(other) - other.is_a?(JoinAssociation) && + other.class == self.class && other.reflection == reflection && other.parent == parent end diff --git a/activerecord/test/cases/associations/cascaded_eager_loading_test.rb b/activerecord/test/cases/associations/cascaded_eager_loading_test.rb index fe558f9..4a59c64 100644 --- a/activerecord/test/cases/associations/cascaded_eager_loading_test.rb +++ b/activerecord/test/cases/associations/cascaded_eager_loading_test.rb @@ -7,9 +7,10 @@ require 'models/categorization' require 'models/company' require 'models/topic' require 'models/reply' +require 'models/person' class CascadedEagerLoadingTest < ActiveRecord::TestCase - fixtures :authors, :mixins, :companies, :posts, :topics, :accounts, :comments, :categorizations + fixtures :authors, :mixins, :companies, :posts, :topics, :accounts, :comments, :categorizations, :people def test_eager_association_loading_with_cascaded_two_levels authors = Author.find(:all, :include=>{:posts=>:comments}, :order=>"authors.id") @@ -37,6 +38,13 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase assert_equal 1, assert_no_queries { authors.size } assert_equal 9, assert_no_queries { authors[0].comments.size } end + + def test_eager_association_loading_grafts_stashed_associations_to_correct_parent + assert_nothing_raised do + Person.eager_load(:primary_contact => :primary_contact).where('primary_contacts_people_2.first_name = ?', 'Susan').all + end + assert_equal people(:michael), Person.eager_load(:primary_contact => :primary_contact).where('primary_contacts_people_2.first_name = ?', 'Susan').first + end def test_eager_association_loading_with_cascaded_two_levels_with_two_has_many_associations authors = Author.find(:all, :include=>{:posts=>[:comments, :categorizations]}, :order=>"authors.id") -- 1.6.4.4