mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
fix exists? to return false if passed nil (which may come from a missing URL param)
This commit is contained in:
@@ -180,7 +180,9 @@ module ActiveRecord
|
||||
# Person.exists?(:name => "David")
|
||||
# Person.exists?(['name LIKE ?', "%#{query}%"])
|
||||
# Person.exists?
|
||||
def exists?(id = nil)
|
||||
def exists?(id = false)
|
||||
return false if id.nil?
|
||||
|
||||
id = id.id if ActiveRecord::Base === id
|
||||
|
||||
join_dependency = construct_join_dependency_for_association_find
|
||||
|
||||
@@ -48,6 +48,15 @@ class FinderTest < ActiveRecord::TestCase
|
||||
assert Topic.exists?
|
||||
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
|
||||
Topic.delete_all
|
||||
assert !Topic.exists?
|
||||
|
||||
Reference in New Issue
Block a user