mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Override #find_by! in core to enable AST caching
This commit is contained in:
parent
58c5261efa
commit
37939e28c9
2 changed files with 26 additions and 0 deletions
|
@ -177,6 +177,10 @@ module ActiveRecord
|
|||
end
|
||||
end
|
||||
|
||||
def find_by!(*args)
|
||||
find_by(*args) or raise RecordNotFound
|
||||
end
|
||||
|
||||
def initialize_generated_modules
|
||||
super
|
||||
|
||||
|
|
|
@ -1047,6 +1047,28 @@ class FinderTest < ActiveRecord::TestCase
|
|||
assert_sql(/^((?!ORDER).)*$/) { Post.find_by(id: posts(:eager_other).id) }
|
||||
end
|
||||
|
||||
test "find_by! with hash conditions returns the first matching record" do
|
||||
assert_equal posts(:eager_other), Post.find_by!(id: posts(:eager_other).id)
|
||||
end
|
||||
|
||||
test "find_by! with non-hash conditions returns the first matching record" do
|
||||
assert_equal posts(:eager_other), Post.find_by!("id = #{posts(:eager_other).id}")
|
||||
end
|
||||
|
||||
test "find_by! with multi-arg conditions returns the first matching record" do
|
||||
assert_equal posts(:eager_other), Post.find_by!('id = ?', posts(:eager_other).id)
|
||||
end
|
||||
|
||||
test "find_by! doesn't have implicit ordering" do
|
||||
assert_sql(/^((?!ORDER).)*$/) { Post.find_by!(id: posts(:eager_other).id) }
|
||||
end
|
||||
|
||||
test "find_by! raises RecordNotFound if the record is missing" do
|
||||
assert_raises(ActiveRecord::RecordNotFound) do
|
||||
Post.find_by!("1 = 0")
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
def bind(statement, *vars)
|
||||
if vars.first.is_a?(Hash)
|
||||
|
|
Loading…
Reference in a new issue