diff options
author | Kohei Suzuki <eagletmt@gmail.com> | 2015-04-10 18:20:02 +0900 |
---|---|---|
committer | Kohei Suzuki <eagletmt@gmail.com> | 2015-04-11 07:48:01 +0900 |
commit | 28375dd621befc495dfc77a6558534951be5ef54 (patch) | |
tree | d2c05ebfc4b482f19c2a287987c55691211dc120 /activerecord/lib/active_record/relation | |
parent | f0d3c920a5aeb3babc35500e13288e148238b65e (diff) | |
download | rails-28375dd621befc495dfc77a6558534951be5ef54.tar.gz rails-28375dd621befc495dfc77a6558534951be5ef54.tar.bz2 rails-28375dd621befc495dfc77a6558534951be5ef54.zip |
Raise ArgumentError when find_by receives no arguments
It fixes the strange error saying undefined method `take'.
```
RelationTest#test_find_by_without_arg_behaves_same_with_find_by({}):
NoMethodError: undefined method `take' for #<ActiveRecord::QueryMethods::WhereChain:0x007f9c55db1d68>
```
Diffstat (limited to 'activerecord/lib/active_record/relation')
-rw-r--r-- | activerecord/lib/active_record/relation/finder_methods.rb | 8 |
1 files changed, 4 insertions, 4 deletions
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 |