Support delegators for `find_by`

Follow up to #40554.
This commit is contained in:
Ryuta Kamizono 2020-11-13 21:56:24 +09:00
parent c9e9346730
commit 19850107cd
2 changed files with 8 additions and 1 deletions

View File

@ -324,7 +324,7 @@ module ActiveRecord
hash = args.first
return super unless Hash === hash
values = hash.values.map! { |value| value.is_a?(Base) ? value.id : value }
values = hash.values.map! { |value| value.respond_to?(:id) ? value.id : value }
return super if values.any? { |v| StatementCache.unsupported_value?(v) }
keys = hash.keys.map! do |key|

View File

@ -830,6 +830,13 @@ class RelationTest < ActiveRecord::TestCase
assert_equal 3, Post.where(author: [decorator.new(author)]).to_a.length
end
def test_find_by_with_delegated_ar_object
decorator = Class.new(SimpleDelegator)
author = Author.first
assert_equal author, Author.find_by(id: decorator.new(author))
assert_equal author, Author.find_by(id: [decorator.new(author)])
end
def test_find_with_list_of_ar
author = Author.first
authors = Author.find([author.id])