提交 7db17040 编写于 作者: F Frederick Cheung

Fix :include of has_many associations with :primary_key option

上级 f4f8923c
......@@ -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]
......
......@@ -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
......
......@@ -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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册