aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2014-11-02 13:53:54 -0700
committerSean Griffin <sean@thoughtbot.com>2014-11-02 13:54:16 -0700
commit76d6d8828007083faa6ccca3ad0feb7ba126ef79 (patch)
tree586bd5e3bc69feb4808b8c8327cf0cfba763fee0 /activerecord/lib/active_record/relation
parentd5902c9e7eaba4db4e79c464d623a7d7e6e2d0e3 (diff)
downloadrails-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/lib/active_record/relation')
-rw-r--r--activerecord/lib/active_record/relation/finder_methods.rb4
1 files changed, 4 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