1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Model#find with hash argument should raise RecordNotFoundError instead of NoMethodError

This commit is contained in:
jasl 2020-09-21 05:15:17 +08:00
parent 7cdc174471
commit 76281aa412
2 changed files with 6 additions and 1 deletions

View file

@ -336,7 +336,7 @@ module ActiveRecord
error = +"Couldn't find #{name}" error = +"Couldn't find #{name}"
error << " with#{conditions}" if conditions error << " with#{conditions}" if conditions
raise RecordNotFound.new(error, name, key) raise RecordNotFound.new(error, name, key)
elsif Array(ids).size == 1 elsif Array.wrap(ids).size == 1
error = "Couldn't find #{name} with '#{key}'=#{ids}#{conditions}" error = "Couldn't find #{name} with '#{key}'=#{ids}#{conditions}"
raise RecordNotFound.new(error, name, key, ids) raise RecordNotFound.new(error, name, key, ids)
else else

View file

@ -44,6 +44,11 @@ class FinderTest < ActiveRecord::TestCase
assert_equal(topics(:first).title, Topic.find(1).title) assert_equal(topics(:first).title, Topic.find(1).title)
end end
def test_find_with_hash_parameter
assert_raises(ActiveRecord::RecordNotFound) { Post.find(foo: "bar") }
assert_raises(ActiveRecord::RecordNotFound) { Post.find(foo: "bar", bar: "baz") }
end
def test_find_with_proc_parameter_and_block def test_find_with_proc_parameter_and_block
exception = assert_raises(RuntimeError) do exception = assert_raises(RuntimeError) do
Topic.all.find(-> { raise "should happen" }) { |e| e.title == "non-existing-title" } Topic.all.find(-> { raise "should happen" }) { |e| e.title == "non-existing-title" }