public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Implement old-skool eagerloading for has_one :through

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
fcheung (author)
Mon Aug 25 15:20:10 -0700 2008
jeremy (committer)
Mon Aug 25 21:23:15 -0700 2008
commit  2dbda11945507a0541d61d13b8c564121c1cd362
tree    fb388c1f8de151e890045146d11dbfe0849bd0fa
parent  172606e21f54fea39af68ede5f55a43deaf3ac68
...
1974
1975
1976
1977
 
1978
1979
1980
...
1998
1999
2000
2001
 
2002
2003
2004
...
1974
1975
1976
 
1977
1978
1979
1980
...
1998
1999
2000
 
2001
2002
2003
2004
0
@@ -1974,7 +1974,7 @@ module ActiveRecord
0
                 @aliased_join_table_name = aliased_table_name_for(reflection.options[:join_table], "_join")
0
               end
0
         
0
-              if reflection.macro == :has_many && reflection.options[:through]
0
+              if [:has_many, :has_one].include?(reflection.macro) && reflection.options[:through]
0
                 @aliased_join_table_name = aliased_table_name_for(reflection.through_reflection.klass.table_name, "_join")
0
               end
0
             end
0
@@ -1998,7 +1998,7 @@ module ActiveRecord
0
                      ]
0
                 when :has_many, :has_one
0
                   case
0
-                    when reflection.macro == :has_many && reflection.options[:through]
0
+                    when reflection.options[:through]
0
                       through_conditions = through_reflection.options[:conditions] ? "AND #{interpolate_sql(sanitize_sql(through_reflection.options[:conditions]))}" : ''
0
 
0
                       jt_foreign_key = jt_as_extra = jt_source_extra = jt_sti_extra = nil
...
75
76
77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
...
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
0
@@ -75,4 +75,20 @@ class HasOneThroughAssociationsTest < ActiveRecord::TestCase
0
     assert_not_nil assert_no_queries {clubs[0].sponsored_member}
0
   end
0
 
0
+  def test_has_one_through_nonpreload_eagerloading
0
+    members = assert_queries(1) do
0
+      Member.find(:all, :include => :club, :conditions => ["members.name = ?", "Groucho Marx"], :order => 'clubs.name') #force fallback
0
+    end
0
+    assert_equal 1, members.size
0
+    assert_not_nil assert_no_queries {members[0].club}
0
+  end
0
+
0
+  def test_has_one_through_nonpreload_eager_loading_through_polymorphic
0
+    members = assert_queries(1) do
0
+      Member.find(:all, :include => :sponsor_club, :conditions => ["members.name = ?", "Groucho Marx"], :order => 'clubs.name') #force fallback
0
+    end
0
+    assert_equal 1, members.size
0
+    assert_not_nil assert_no_queries {members[0].sponsor_club}
0
+  end
0
+
0
 end

Comments