Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix :include of has_one with :primary_key option

  • Loading branch information...
commit f9cab0e503a4721c9d0369f89bb85c6e658f778c 1 parent 7db1704
@fcheung fcheung authored
View
2  activerecord/lib/active_record/association_preload.rb
@@ -195,7 +195,7 @@ def preload_has_and_belongs_to_many_association(records, reflection, preload_opt
def preload_has_one_association(records, reflection, preload_options={})
return if records.first.send("loaded_#{reflection.name}?")
- id_to_record_map, ids = construct_id_map(records)
+ id_to_record_map, ids = construct_id_map(records, reflection.options[:primary_key])
options = reflection.options
records.each {|record| record.send("set_#{reflection.name}_target", nil)}
if options[:through]
View
16 activerecord/test/cases/associations/eager_test.rb
@@ -803,4 +803,20 @@ def test_include_has_many_using_primary_key
end
end
+ def test_preload_has_one_using_primary_key
+ expected = Firm.find(:first).account_using_primary_key
+ firm = Firm.find :first, :include => :account_using_primary_key
+ assert_no_queries do
+ assert_equal expected, firm.account_using_primary_key
+ end
+ end
+
+ def test_include_has_one_using_primary_key
+ expected = Firm.find(1).account_using_primary_key
+ firm = Firm.find(:all, :include => :account_using_primary_key, :order => 'accounts.id').detect {|f| f.id == 1}
+ assert_no_queries do
+ assert_equal expected, firm.account_using_primary_key
+ end
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.