aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2006-08-03 22:06:33 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2006-08-03 22:06:33 +0000
commit58ebf302b206ed30568da60d7f3b124b26e9325b (patch)
tree16c37488980a7597b68bdf5056089197c31cf2f3 /activerecord/lib/active_record
parent149d1815625e2df4709415c1129fbdd413f9ca44 (diff)
downloadrails-58ebf302b206ed30568da60d7f3b124b26e9325b.tar.gz
rails-58ebf302b206ed30568da60d7f3b124b26e9325b.tar.bz2
rails-58ebf302b206ed30568da60d7f3b124b26e9325b.zip
The exists? class method should treat a string argument as an id rather than as conditions. Closes #5698.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4655 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record')
-rwxr-xr-xactiverecord/lib/active_record/base.rb18
1 files changed, 15 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index d704bf5206..40dfb387ce 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -426,10 +426,13 @@ module ActiveRecord #:nodoc:
# You can also pass a set of SQL conditions.
# Example:
# Person.exists?(5)
+ # Person.exists?('5')
# Person.exists?(:name => "David")
- def exists?(conditions)
- conditions = ["#{primary_key} = ?", conditions] if conditions.is_a?(Fixnum)
- !find(:first, :conditions => conditions).nil? rescue false
+ # Person.exists?(['name LIKE ?', "%#{query}%"])
+ def exists?(id_or_conditions)
+ !find(:first, :conditions => expand_id_conditions(id_or_conditions)).nil?
+ rescue ActiveRecord::ActiveRecordError
+ false
end
# Creates an object, instantly saves it as a record (if the validation permits it), and returns it. If the save
@@ -1224,6 +1227,15 @@ module ActiveRecord #:nodoc:
end
end
+ # Interpret Array and Hash as conditions and anything else as an id.
+ def expand_id_conditions(id_or_conditions)
+ case id_or_conditions
+ when Array, Hash then id_or_conditions
+ else construct_conditions_from_arguments([primary_key], [id_or_conditions])
+ end
+ end
+
+
# Defines an "attribute" method (like #inheritance_column or
# #table_name). A new (class) method will be created with the
# given name. If a value is specified, the new method will