mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #40261 from jasl/fix-ar-find-with-hash
Model#find with hash should not raise NoMethodError
This commit is contained in:
commit
e09d4f95d6
2 changed files with 6 additions and 1 deletions
|
@ -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
|
||||||
|
|
|
@ -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" }
|
||||||
|
|
Loading…
Reference in a new issue