diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-09-07 00:38:51 -0700 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2011-09-07 00:38:51 -0700 |
commit | db8d54ebf5102d2f30c0c332ea852ad284702ef3 (patch) | |
tree | 5700b21c7dc088f9506f49e76dfb249826004e2f | |
parent | 4edf6ea0eae9cb8be46a2e963e047eb89a0f5531 (diff) | |
parent | d48dd18bb2a3d0c46708a9ee217909783b997cb2 (diff) | |
download | rails-db8d54ebf5102d2f30c0c332ea852ad284702ef3.tar.gz rails-db8d54ebf5102d2f30c0c332ea852ad284702ef3.tar.bz2 rails-db8d54ebf5102d2f30c0c332ea852ad284702ef3.zip |
Merge pull request #2485 from akaspick/exists_fix
fix exists? to return false if passed nil (which may come from a missing
-rw-r--r-- | activerecord/lib/active_record/relation/finder_methods.rb | 4 | ||||
-rw-r--r-- | activerecord/test/cases/finder_test.rb | 9 |
2 files changed, 12 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 83d650d3f4..7eeb3dde70 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -184,7 +184,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 diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index d840d38678..47bf2d007f 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -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? |