public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Fix :include of has_many associations with :primary_key option
fcheung (author)
Fri Dec 26 14:53:07 -0800 2008
commit  7db1704068b86fb2212388b14b4963526bacfa5d
tree    615165073929beb1f1c0ad67fd8f146446ac2c56
parent  f4f8923cf0ef5bd31f9e98cecf4603d0c4bde296
...
229
230
231
232
 
233
234
235
...
229
230
231
 
232
233
234
235
0
@@ -229,7 +229,7 @@ module ActiveRecord
0
         options = reflection.options
0
 
0
         primary_key_name = reflection.through_reflection_primary_key_name
0
-        id_to_record_map, ids = construct_id_map(records, primary_key_name)
0
+        id_to_record_map, ids = construct_id_map(records, primary_key_name || reflection.options[:primary_key])
0
         records.each {|record| record.send(reflection.name).loaded}
0
 
0
         if options[:through]
...
2171
2172
2173
2174
 
2175
2176
2177
...
2171
2172
2173
 
2174
2175
2176
2177
0
@@ -2171,7 +2171,7 @@ module ActiveRecord
0
                         aliased_table_name,
0
                         foreign_key,
0
                         parent.aliased_table_name,
0
-                        parent.primary_key
0
+                        reflection.options[:primary_key] || parent.primary_key
0
                       ]
0
                   end
0
                 when :belongs_to
...
786
787
788
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
789
...
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
0
@@ -786,4 +786,21 @@ class EagerAssociationTest < ActiveRecord::TestCase
0
       assert_equal Person.find(person.id).agents, person.agents
0
     end
0
   end
0
+
0
+  def test_preload_has_many_using_primary_key
0
+    expected = Firm.find(:first).clients_using_primary_key.to_a
0
+    firm = Firm.find :first, :include => :clients_using_primary_key
0
+    assert_no_queries do
0
+      assert_equal expected, firm.clients_using_primary_key
0
+    end
0
+  end
0
+
0
+  def test_include_has_many_using_primary_key
0
+    expected = Firm.find(1).clients_using_primary_key.sort_by &:name
0
+    firm = Firm.find 1, :include => :clients_using_primary_key, :order => 'clients_using_primary_keys_companies.name'
0
+    assert_no_queries do
0
+      assert_equal expected, firm.clients_using_primary_key
0
+    end
0
+  end
0
+  
0
 end

Comments