提交 db8d54eb 编写于 作者: J Jon Leighton

Merge pull request #2485 from akaspick/exists_fix

fix exists? to return false if passed nil (which may come from a missing 
...@@ -184,7 +184,9 @@ def all(*args) ...@@ -184,7 +184,9 @@ def all(*args)
# Person.exists?(:name => "David") # Person.exists?(:name => "David")
# Person.exists?(['name LIKE ?', "%#{query}%"]) # Person.exists?(['name LIKE ?', "%#{query}%"])
# Person.exists? # Person.exists?
def exists?(id = nil) def exists?(id = false)
return false if id.nil?
id = id.id if ActiveRecord::Base === id id = id.id if ActiveRecord::Base === id
join_dependency = construct_join_dependency_for_association_find join_dependency = construct_join_dependency_for_association_find
......
...@@ -48,6 +48,15 @@ def test_exists_returns_true_with_one_record_and_no_args ...@@ -48,6 +48,15 @@ def test_exists_returns_true_with_one_record_and_no_args
assert Topic.exists? assert Topic.exists?
end end
# exists? should handle nil for id's that come from URLs and always return false
# (example: Topic.exists?(params[:id])) where params[:id] is nil
def test_exists_with_nil_arg
assert !Topic.exists?(nil)
assert Topic.exists?
assert !Topic.first.replies.exists?(nil)
assert Topic.first.replies.exists?
end
def test_does_not_exist_with_empty_table_and_no_args_given def test_does_not_exist_with_empty_table_and_no_args_given
Topic.delete_all Topic.delete_all
assert !Topic.exists? assert !Topic.exists?
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册