diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2006-08-03 17:16:43 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2006-08-03 17:16:43 +0000 |
commit | 8085cbfd0898676ffaea2f2d1c2616e47a37e5e1 (patch) | |
tree | 5064c9757830db87b260eca6d284dabd42c1fe94 /activerecord/lib/active_record | |
parent | 29ae3e90988a9edf09d85ed32d5aac76fa199341 (diff) | |
download | rails-8085cbfd0898676ffaea2f2d1c2616e47a37e5e1.tar.gz rails-8085cbfd0898676ffaea2f2d1c2616e47a37e5e1.tar.bz2 rails-8085cbfd0898676ffaea2f2d1c2616e47a37e5e1.zip |
Added support for conditions on Base.exists? (closes #5689) [josh@joshpeek.com]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4651 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record')
-rwxr-xr-x | activerecord/lib/active_record/base.rb | 7 |
1 files changed, 5 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 52b7df7fe8..d704bf5206 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -423,10 +423,13 @@ module ActiveRecord #:nodoc: end # Returns true if the given +id+ represents the primary key of a record in the database, false otherwise. + # You can also pass a set of SQL conditions. # Example: # Person.exists?(5) - def exists?(id) - !find(:first, :conditions => ["#{primary_key} = ?", id]).nil? rescue false + # Person.exists?(:name => "David") + def exists?(conditions) + conditions = ["#{primary_key} = ?", conditions] if conditions.is_a?(Fixnum) + !find(:first, :conditions => conditions).nil? rescue false end # Creates an object, instantly saves it as a record (if the validation permits it), and returns it. If the save |