提交 49d94cc4 编写于 作者: R Rafael Mendonça França

Merge pull request #12362 from arthurnn/inverse_on_find

fix .find when inverse is loaded
* Fixed `ActiveRecord::Associations::CollectionAssociation#find`
when using `has_many` association with `:inverse_of` and finding an array of one element,
it should return an array of one element too.
*arthurnn*
* Callbacks on has_many should access the in memory parent if a inverse_of is set.
*arthurnn*
......
......@@ -80,14 +80,13 @@ def find(*args)
load_target.find(*args) { |*block_args| yield(*block_args) }
else
if options[:inverse_of] && loaded?
args = args.flatten
raise RecordNotFound, "Couldn't find #{scope.klass.name} without an ID" if args.blank?
args_flatten = args.flatten
raise RecordNotFound, "Couldn't find #{scope.klass.name} without an ID" if args_flatten.blank?
result = find_by_scan(*args)
result_size = Array(result).size
if !result || result_size != args.size
scope.raise_record_not_found_exception!(args, result_size, args.size)
if !result || result_size != args_flatten.size
scope.raise_record_not_found_exception!(args_flatten, result_size, args_flatten.size)
else
result
end
......
......@@ -337,6 +337,18 @@ def test_find_ids
assert_raise(ActiveRecord::RecordNotFound) { firm.clients.find(2, 99) }
end
def test_find_ids_and_inverse_of
force_signal37_to_load_all_clients_of_firm
firm = companies(:first_firm)
client = firm.clients_of_firm.find(3)
assert_kind_of Client, client
client_ary = firm.clients_of_firm.find([3])
assert_kind_of Array, client_ary
assert_equal client, client_ary.first
end
def test_find_all
firm = Firm.all.merge!(:order => "id").first
assert_equal 2, firm.clients.where("#{QUOTED_TYPE} = 'Client'").to_a.length
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册