diff options
author | Yves Senn <yves.senn@gmail.com> | 2015-04-28 11:03:18 +0200 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2015-04-28 11:06:53 +0200 |
commit | cdf3548a452bbe5cddaad0a398d4266b36f9e388 (patch) | |
tree | 690f96ea92f7fd0bee4f52341bec3f9c3674b7e6 | |
parent | 7edf52062a1f12bbfdf831665e8c36bd24db793d (diff) | |
parent | 28375dd621befc495dfc77a6558534951be5ef54 (diff) | |
download | rails-cdf3548a452bbe5cddaad0a398d4266b36f9e388.tar.gz rails-cdf3548a452bbe5cddaad0a398d4266b36f9e388.tar.bz2 rails-cdf3548a452bbe5cddaad0a398d4266b36f9e388.zip |
Merge pull request #19718 from eagletmt/find_by-without-arg
Raise ArgumentError when find_by receives no arguments
-rw-r--r-- | activerecord/CHANGELOG.md | 5 | ||||
-rw-r--r-- | activerecord/lib/active_record/relation/finder_methods.rb | 8 | ||||
-rw-r--r-- | activerecord/test/cases/relations_test.rb | 8 |
3 files changed, 17 insertions, 4 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index fa2b7f0c0f..53e2c4af79 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,8 @@ +* `find_by` and `find_by!` raise `ArgumentError` when called without + arguments. + + *Kohei Suzuki* + * Revert behavior of `db:schema:load` back to loading the full environment. This ensures that initializers are run. diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 6a3a56f1cc..576a32bf75 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -77,16 +77,16 @@ module ActiveRecord # # Post.find_by name: 'Spartacus', rating: 4 # Post.find_by "published_at < ?", 2.weeks.ago - def find_by(*args) - where(*args).take + def find_by(arg, *args) + where(arg, *args).take rescue RangeError nil end # Like <tt>find_by</tt>, except that if no record is found, raises # an <tt>ActiveRecord::RecordNotFound</tt> error. - def find_by!(*args) - where(*args).take! + def find_by!(arg, *args) + where(arg, *args).take! rescue RangeError raise RecordNotFound, "Couldn't find #{@klass.name} with an out of range value" end diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 0cf44388fa..b8e2041b6d 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -1657,6 +1657,10 @@ class RelationTest < ActiveRecord::TestCase assert_sql(/^((?!ORDER).)*$/) { Post.all.find_by(author_id: 2) } end + test "find_by requires at least one argument" do + assert_raises(ArgumentError) { Post.all.find_by } + end + test "find_by! with hash conditions returns the first matching record" do assert_equal posts(:eager_other), Post.order(:id).find_by!(author_id: 2) end @@ -1679,6 +1683,10 @@ class RelationTest < ActiveRecord::TestCase end end + test "find_by! requires at least one argument" do + assert_raises(ArgumentError) { Post.all.find_by! } + end + test "loaded relations cannot be mutated by multi value methods" do relation = Post.all relation.to_a |