Skip to content
This repository
Browse code

Fix multiple self-referencing eager loads failing to join multiple times

[#4679 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information...
commit a4eaa1fd39f93eff975bfd8a5cc4c7dfc3f18aa7 1 parent e404490
Ernie Miller authored jeremy committed
4  activerecord/lib/active_record/associations.rb
@@ -1903,7 +1903,7 @@ def initialize(active_record, joins = nil)
1903 1903
             end
1904 1904
 
1905 1905
             def ==(other)
1906  
-              other.is_a?(JoinBase) &&
  1906
+              other.class == self.class &&
1907 1907
               other.active_record == active_record &&
1908 1908
               other.table_joins == table_joins
1909 1909
             end
@@ -1974,7 +1974,7 @@ def initialize(reflection, join_dependency, parent = nil)
1974 1974
             end
1975 1975
 
1976 1976
             def ==(other)
1977  
-              other.is_a?(JoinAssociation) &&
  1977
+              other.class == self.class &&
1978 1978
               other.reflection == reflection &&
1979 1979
               other.parent == parent
1980 1980
             end
10  activerecord/test/cases/associations/cascaded_eager_loading_test.rb
@@ -7,9 +7,10 @@
7 7
 require 'models/company'
8 8
 require 'models/topic'
9 9
 require 'models/reply'
  10
+require 'models/person'
10 11
 
11 12
 class CascadedEagerLoadingTest < ActiveRecord::TestCase
12  
-  fixtures :authors, :mixins, :companies, :posts, :topics, :accounts, :comments, :categorizations
  13
+  fixtures :authors, :mixins, :companies, :posts, :topics, :accounts, :comments, :categorizations, :people
13 14
 
14 15
   def test_eager_association_loading_with_cascaded_two_levels
15 16
     authors = Author.find(:all, :include=>{:posts=>:comments}, :order=>"authors.id")
@@ -38,6 +39,13 @@ def test_eager_association_loading_with_hmt_does_not_table_name_collide_when_joi
38 39
     assert_equal 9, assert_no_queries { authors[0].comments.size }
39 40
   end
40 41
 
  42
+  def test_eager_association_loading_grafts_stashed_associations_to_correct_parent
  43
+    assert_nothing_raised do
  44
+      Person.eager_load(:primary_contact => :primary_contact).where('primary_contacts_people_2.first_name = ?', 'Susan').all
  45
+    end
  46
+    assert_equal people(:michael), Person.eager_load(:primary_contact => :primary_contact).where('primary_contacts_people_2.first_name = ?', 'Susan').first
  47
+  end
  48
+
41 49
   def test_eager_association_loading_with_cascaded_two_levels_with_two_has_many_associations
42 50
     authors = Author.find(:all, :include=>{:posts=>[:comments, :categorizations]}, :order=>"authors.id")
43 51
     assert_equal 2, authors.size

0 notes on commit a4eaa1f

Please sign in to comment.
Something went wrong with that request. Please try again.