Skip to content

Commit

Permalink
Fix :include of has_one with :primary_key option
Browse files Browse the repository at this point in the history
  • Loading branch information
fcheung committed Dec 26, 2008
1 parent 7db1704 commit f9cab0e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/association_preload.rb
Expand Up @@ -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={}) def preload_has_one_association(records, reflection, preload_options={})
return if records.first.send("loaded_#{reflection.name}?") 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 options = reflection.options
records.each {|record| record.send("set_#{reflection.name}_target", nil)} records.each {|record| record.send("set_#{reflection.name}_target", nil)}
if options[:through] if options[:through]
Expand Down
16 changes: 16 additions & 0 deletions activerecord/test/cases/associations/eager_test.rb
Expand Up @@ -803,4 +803,20 @@ def test_include_has_many_using_primary_key
end end
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 end

0 comments on commit f9cab0e

Please sign in to comment.