diff options
author | Sean Griffin <sean@thoughtbot.com> | 2014-11-02 13:53:54 -0700 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2014-11-02 13:54:16 -0700 |
commit | 76d6d8828007083faa6ccca3ad0feb7ba126ef79 (patch) | |
tree | 586bd5e3bc69feb4808b8c8327cf0cfba763fee0 /activerecord | |
parent | d5902c9e7eaba4db4e79c464d623a7d7e6e2d0e3 (diff) | |
download | rails-76d6d8828007083faa6ccca3ad0feb7ba126ef79.tar.gz rails-76d6d8828007083faa6ccca3ad0feb7ba126ef79.tar.bz2 rails-76d6d8828007083faa6ccca3ad0feb7ba126ef79.zip |
Handle `RangeError` from casting in `find_by` and `find_by!` on Relation
We should not behave differently just because a class has a default
scope.
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/relation/finder_methods.rb | 4 | ||||
-rw-r--r-- | activerecord/test/cases/finder_test.rb | 10 |
2 files changed, 14 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 6dd932e530..eacae73ebb 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -82,12 +82,16 @@ module ActiveRecord # Post.find_by "published_at < ?", 2.weeks.ago def find_by(*args) where(*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! + rescue RangeError + raise RecordNotFound, "Couldn't find #{@klass.name} with an out of range value" end # Gives a record (or N records if a parameter is supplied) without any implied diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index d1ff8516e8..dc73faa5be 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -210,6 +210,16 @@ class FinderTest < ActiveRecord::TestCase assert_nil Topic.find_by_id('9999999999999999999999999999999') end + def test_find_on_relation_with_large_number + assert_nil Topic.where('1=1').find_by(id: 9999999999999999999999999999999) + end + + def test_find_by_bang_on_relation_with_large_number + assert_raises(ActiveRecord::RecordNotFound) do + Topic.where('1=1').find_by!(id: 9999999999999999999999999999999) + end + end + def test_find_an_empty_array assert_equal [], Topic.find([]) end |