Permalink
Browse files

Merge pull request #2919 from akaspick/exists_fix_3_0

fix exists? to return false if passed nil (backport to 3-0-stable)
  • Loading branch information...
2 parents 59a64a8 + 3e00e49 commit 9ef9f9884bf32f6b73c868887f125f1dd43f9ac6 @jonleighton jonleighton committed Sep 7, 2011
@@ -3,6 +3,9 @@
* Psych errors with poor yaml formatting are proxied. Fixes GH #2645 and
GH #2731
+* Fix ActiveRecord#exists? when passsed a nil value
+
+
* Rails 3.0.10
* Magic encoding comment added to schema.rb files
@@ -168,7 +168,9 @@ def all(*args)
# 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
@@ -98,6 +98,15 @@ def test_exists_returns_true_with_one_record_and_no_args
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?

0 comments on commit 9ef9f98

Please sign in to comment.