public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Fix :include of has_one with :primary_key option
fcheung (author)
Fri Dec 26 15:26:37 -0800 2008
commit  f9cab0e503a4721c9d0369f89bb85c6e658f778c
tree    8d0c579bd0f75b824779b3a5ba76c3f39af157a6
parent  7db1704068b86fb2212388b14b4963526bacfa5d
...
195
196
197
198
 
199
200
201
...
195
196
197
 
198
199
200
201
0
@@ -195,7 +195,7 @@ module ActiveRecord
0
 
0
       def preload_has_one_association(records, reflection, preload_options={})
0
         return if records.first.send("loaded_#{reflection.name}?")
0
-        id_to_record_map, ids = construct_id_map(records)
0
+        id_to_record_map, ids = construct_id_map(records, reflection.options[:primary_key])
0
         options = reflection.options
0
         records.each {|record| record.send("set_#{reflection.name}_target", nil)}
0
         if options[:through]
...
803
804
805
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
806
...
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
0
@@ -803,4 +803,20 @@ class EagerAssociationTest < ActiveRecord::TestCase
0
     end
0
   end
0
   
0
+  def test_preload_has_one_using_primary_key
0
+    expected = Firm.find(:first).account_using_primary_key
0
+    firm = Firm.find :first, :include => :account_using_primary_key
0
+    assert_no_queries do
0
+      assert_equal expected, firm.account_using_primary_key
0
+    end
0
+  end
0
+
0
+  def test_include_has_one_using_primary_key
0
+    expected = Firm.find(1).account_using_primary_key
0
+    firm = Firm.find(:all, :include => :account_using_primary_key, :order => 'accounts.id').detect {|f| f.id == 1}
0
+    assert_no_queries do
0
+      assert_equal expected, firm.account_using_primary_key
0
+    end
0
+  end
0
+  
0
 end

Comments