diff options
author | Frederick Cheung <frederick.cheung@gmail.com> | 2008-12-26 23:26:37 +0000 |
---|---|---|
committer | Frederick Cheung <frederick.cheung@gmail.com> | 2008-12-26 23:26:37 +0000 |
commit | f9cab0e503a4721c9d0369f89bb85c6e658f778c (patch) | |
tree | 8d0c579bd0f75b824779b3a5ba76c3f39af157a6 | |
parent | 7db1704068b86fb2212388b14b4963526bacfa5d (diff) | |
download | rails-f9cab0e503a4721c9d0369f89bb85c6e658f778c.tar.gz rails-f9cab0e503a4721c9d0369f89bb85c6e658f778c.tar.bz2 rails-f9cab0e503a4721c9d0369f89bb85c6e658f778c.zip |
Fix :include of has_one with :primary_key option
-rw-r--r-- | activerecord/lib/active_record/association_preload.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/associations/eager_test.rb | 16 |
2 files changed, 17 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/association_preload.rb b/activerecord/lib/active_record/association_preload.rb index 195d2fb4f8..e4ab69aa1b 100644 --- a/activerecord/lib/active_record/association_preload.rb +++ b/activerecord/lib/active_record/association_preload.rb @@ -195,7 +195,7 @@ module ActiveRecord 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] diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index ff3d6ece05..14099d4176 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -803,4 +803,20 @@ class EagerAssociationTest < ActiveRecord::TestCase 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 |