diff options
-rw-r--r-- | activerecord/CHANGELOG | 6 | ||||
-rwxr-xr-x | activerecord/lib/active_record/base.rb | 7 | ||||
-rw-r--r-- | activerecord/test/finder_test.rb | 3 |
3 files changed, 14 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 76973e4529..70cf37a7ac 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,11 @@ *SVN* +* Added support for conditions on Base.exists? #5689 [josh@joshpeek.com]. Examples: + + assert (Topic.exists?(:author_name => "David")) + assert (Topic.exists?(:author_name => "Mary", :approved => true)) + assert (Topic.exists?(["parent_id = ?", 1])) + * Schema dumper quotes date :default values. [Dave Thomas] * Calculate sum with SQL, not Enumerable on HasManyThrough Associations. [Dan Peterson] 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 diff --git a/activerecord/test/finder_test.rb b/activerecord/test/finder_test.rb index 23b6508f99..7e950dac41 100644 --- a/activerecord/test/finder_test.rb +++ b/activerecord/test/finder_test.rb @@ -21,6 +21,9 @@ class FinderTest < Test::Unit::TestCase def test_exists assert (Topic.exists?(1)) + assert (Topic.exists?(:author_name => "David")) + assert (Topic.exists?(:author_name => "Mary", :approved => true)) + assert (Topic.exists?(["parent_id = ?", 1])) assert !(Topic.exists?(45)) assert !(Topic.exists?("foo")) assert !(Topic.exists?([1,2])) |