public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Fix preloading of has_one through associations

[#903 state:resolved]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
fcheung (author)
Mon Aug 25 14:36:19 -0700 2008
jeremy (committer)
Mon Aug 25 20:50:39 -0700 2008
commit  fdeeeaea61bb34040f8da03afa9876171db4b17e
tree    f5b28312b3db3e6ddbc76e08a3be6dba54442a67
parent  f47767696479391fbdb967f1321478d4c338cecc
...
51
52
53
54
55
56
 
57
58
59
...
112
113
114
 
115
116
117
118
119
...
126
127
128
129
130
131
132
133
...
51
52
53
 
 
 
54
55
56
57
...
110
111
112
113
114
 
115
116
117
...
124
125
126
 
 
127
128
129
0
@@ -51,9 +51,7 @@ module ActiveRecord
0
       
0
       def add_preloaded_record_to_collection(parent_records, reflection_name, associated_record)
0
         parent_records.each do |parent_record|
0
-          association_proxy = parent_record.send(reflection_name)
0
-          association_proxy.loaded
0
-          association_proxy.target = associated_record
0
+          parent_record.send("set_#{reflection_name}_target", associated_record)
0
         end
0
       end
0
 
0
@@ -112,8 +110,8 @@ module ActiveRecord
0
       def preload_has_one_association(records, reflection, preload_options={})
0
         id_to_record_map, ids = construct_id_map(records)        
0
         options = reflection.options
0
+        records.each {|record| record.send("set_#{reflection.name}_target", nil)}
0
         if options[:through]
0
-          records.each {|record| record.send(reflection.name) && record.send(reflection.name).loaded}
0
           through_records = preload_through_records(records, reflection, options[:through])
0
           through_reflection = reflections[options[:through]]
0
           through_primary_key = through_reflection.primary_key_name
0
@@ -126,8 +124,6 @@ module ActiveRecord
0
             end
0
           end
0
         else
0
-          records.each {|record| record.send("set_#{reflection.name}_target", nil)}
0
-
0
           set_association_single_records(id_to_record_map, reflection.name, find_associated_records(ids, reflection, preload_options), reflection.primary_key_name)
0
         end
0
       end
...
44
45
46
47
 
48
49
50
51
 
52
53
 
 
 
54
55
56
57
 
58
59
 
 
 
60
61
62
...
44
45
46
 
47
48
49
50
 
51
52
 
53
54
55
56
57
58
 
59
60
 
61
62
63
64
65
66
0
@@ -44,19 +44,23 @@ class HasOneThroughAssociationsTest < ActiveRecord::TestCase
0
   def test_has_one_through_polymorphic
0
     assert_equal clubs(:moustache_club), @member.sponsor_club
0
   end
0
-  
0
+
0
   def has_one_through_to_has_many
0
     assert_equal 2, @member.fellow_members.size
0
   end
0
-  
0
+
0
   def test_has_one_through_eager_loading
0
-    members = Member.find(:all, :include => :club, :conditions => ["name = ?", "Groucho Marx"])
0
+    members = assert_queries(3) do #base table, through table, clubs table
0
+      Member.find(:all, :include => :club, :conditions => ["name = ?", "Groucho Marx"])
0
+    end
0
     assert_equal 1, members.size
0
     assert_not_nil assert_no_queries {members[0].club}
0
   end
0
-  
0
+
0
   def test_has_one_through_eager_loading_through_polymorphic
0
-    members = Member.find(:all, :include => :sponsor_club, :conditions => ["name = ?", "Groucho Marx"])
0
+    members = assert_queries(3) do #base table, through table, clubs table
0
+      Member.find(:all, :include => :sponsor_club, :conditions => ["name = ?", "Groucho Marx"])
0
+    end
0
     assert_equal 1, members.size
0
     assert_not_nil assert_no_queries {members[0].sponsor_club}    
0
   end

Comments