提交 76d6d882 编写于 作者: S Sean Griffin

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.
上级 d5902c9e
......@@ -82,12 +82,16 @@ def find(*args)
# 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
......
......@@ -210,6 +210,16 @@ def test_find_by_id_with_large_number
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册