From 76281aa412b046b655324bd3a0ab1c067afba1dd Mon Sep 17 00:00:00 2001 From: jasl Date: Mon, 21 Sep 2020 05:15:17 +0800 Subject: [PATCH] Model#find with hash argument should raise RecordNotFoundError instead of NoMethodError --- activerecord/lib/active_record/relation/finder_methods.rb | 2 +- activerecord/test/cases/finder_test.rb | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index c7d46ace93..053c62887b 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -336,7 +336,7 @@ module ActiveRecord error = +"Couldn't find #{name}" error << " with#{conditions}" if conditions 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}" raise RecordNotFound.new(error, name, key, ids) else diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index c60684f1be..980672e9bd 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -44,6 +44,11 @@ class FinderTest < ActiveRecord::TestCase assert_equal(topics(:first).title, Topic.find(1).title) 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 exception = assert_raises(RuntimeError) do Topic.all.find(-> { raise "should happen" }) { |e| e.title == "non-existing-title" }