Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix :include of has_many associations with :primary_key option

  • Loading branch information...
commit 7db1704068b86fb2212388b14b4963526bacfa5d 1 parent f4f8923
@fcheung fcheung authored
View
2  activerecord/lib/active_record/association_preload.rb
@@ -229,7 +229,7 @@ def preload_has_many_association(records, reflection, preload_options={})
options = reflection.options
primary_key_name = reflection.through_reflection_primary_key_name
- id_to_record_map, ids = construct_id_map(records, primary_key_name)
+ id_to_record_map, ids = construct_id_map(records, primary_key_name || reflection.options[:primary_key])
records.each {|record| record.send(reflection.name).loaded}
if options[:through]
View
2  activerecord/lib/active_record/associations.rb
@@ -2171,7 +2171,7 @@ def association_join
aliased_table_name,
foreign_key,
parent.aliased_table_name,
- parent.primary_key
+ reflection.options[:primary_key] || parent.primary_key
]
end
when :belongs_to
View
17 activerecord/test/cases/associations/eager_test.rb
@@ -786,4 +786,21 @@ def test_preload_has_many_uses_exclusive_scope
assert_equal Person.find(person.id).agents, person.agents
end
end
+
+ def test_preload_has_many_using_primary_key
+ expected = Firm.find(:first).clients_using_primary_key.to_a
+ firm = Firm.find :first, :include => :clients_using_primary_key
+ assert_no_queries do
+ assert_equal expected, firm.clients_using_primary_key
+ end
+ end
+
+ def test_include_has_many_using_primary_key
+ expected = Firm.find(1).clients_using_primary_key.sort_by &:name
+ firm = Firm.find 1, :include => :clients_using_primary_key, :order => 'clients_using_primary_keys_companies.name'
+ assert_no_queries do
+ assert_equal expected, firm.clients_using_primary_key
+ end
+ end
+
end

0 comments on commit 7db1704

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