aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-03-17 14:12:25 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-03-17 14:12:25 +0000
commit2f944123f32ad37efc84f13de25350beb7f8fed6 (patch)
treeb0bb91ac280fe3bffc910fe7d954c232810712fd /activerecord/lib
parent9776bfc8a0e110dce751d0e78305432a155b93cd (diff)
downloadrails-2f944123f32ad37efc84f13de25350beb7f8fed6.tar.gz
rails-2f944123f32ad37efc84f13de25350beb7f8fed6.tar.bz2
rails-2f944123f32ad37efc84f13de25350beb7f8fed6.zip
Added Base.exists?(id) that'll return true if an object of the class with the given id exists #854 [stian@grytoyr.net]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@913 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib')
-rwxr-xr-xactiverecord/lib/active_record/base.rb7
1 files changed, 7 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 7fbef9f07c..f85cad1f98 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -326,6 +326,13 @@ module ActiveRecord #:nodoc:
end
end
+ # Returns true if the given +id+ represents the primary key of a record in the database, false otherwise.
+ # Example:
+ # Person.exists?(5)
+ def exists?(id)
+ !find_first("#{primary_key} = #{sanitize(id)}").nil?
+ end
+
# This method is deprecated in favor of find with the :conditions option.
# Works like find, but the record matching +id+ must also meet the +conditions+.
# +RecordNotFound+ is raised if no record can be found matching the +id+ or meeting the condition.