Skip to content
This repository
Browse code

Fix :include of has_many associations with :primary_key option

  • Loading branch information...
commit 7db1704068b86fb2212388b14b4963526bacfa5d 1 parent f4f8923
Frederick Cheung authored December 26, 2008
2  activerecord/lib/active_record/association_preload.rb
@@ -229,7 +229,7 @@ def preload_has_many_association(records, reflection, preload_options={})
229 229
         options = reflection.options
230 230
 
231 231
         primary_key_name = reflection.through_reflection_primary_key_name
232  
-        id_to_record_map, ids = construct_id_map(records, primary_key_name)
  232
+        id_to_record_map, ids = construct_id_map(records, primary_key_name || reflection.options[:primary_key])
233 233
         records.each {|record| record.send(reflection.name).loaded}
234 234
 
235 235
         if options[:through]
2  activerecord/lib/active_record/associations.rb
@@ -2171,7 +2171,7 @@ def association_join
2171 2171
                         aliased_table_name,
2172 2172
                         foreign_key,
2173 2173
                         parent.aliased_table_name,
2174  
-                        parent.primary_key
  2174
+                        reflection.options[:primary_key] || parent.primary_key
2175 2175
                       ]
2176 2176
                   end
2177 2177
                 when :belongs_to
17  activerecord/test/cases/associations/eager_test.rb
@@ -786,4 +786,21 @@ def test_preload_has_many_uses_exclusive_scope
786 786
       assert_equal Person.find(person.id).agents, person.agents
787 787
     end
788 788
   end
  789
+
  790
+  def test_preload_has_many_using_primary_key
  791
+    expected = Firm.find(:first).clients_using_primary_key.to_a
  792
+    firm = Firm.find :first, :include => :clients_using_primary_key
  793
+    assert_no_queries do
  794
+      assert_equal expected, firm.clients_using_primary_key
  795
+    end
  796
+  end
  797
+
  798
+  def test_include_has_many_using_primary_key
  799
+    expected = Firm.find(1).clients_using_primary_key.sort_by &:name
  800
+    firm = Firm.find 1, :include => :clients_using_primary_key, :order => 'clients_using_primary_keys_companies.name'
  801
+    assert_no_queries do
  802
+      assert_equal expected, firm.clients_using_primary_key
  803
+    end
  804
+  end
  805
+  
789 806
 end

0 notes on commit 7db1704

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