Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #2921 from akaspick/exists_fix_3_1

fix exists? to return false if passed nil (backport to 3-1-stable)
  • Loading branch information...
commit 6a18262b4b5d97788ea738aad730d8851912c2f2 2 parents 76993f6 + 8f2f39b
Santiago Pastorino spastorino authored
2  actionpack/CHANGELOG
View
@@ -13,6 +13,8 @@
* Fixed stylesheet_link_tag and javascript_include_tag to respect additional options passed by the users when debug is on. [Guillermo Iguaran]
+* Fix ActiveRecord#exists? when passsed a nil value
+
*Rails 3.1.0 (August 30, 2011)*
4 activerecord/lib/active_record/relation/finder_methods.rb
View
@@ -184,7 +184,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
9 activerecord/test/cases/finder_test.rb
View
@@ -48,6 +48,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?
Please sign in to comment.
Something went wrong with that request. Please try again.