Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 4651

Show
Ignore:
Timestamp:
08/03/06 17:16:43 (2 years ago)
Author:
david
Message:

Added support for conditions on Base.exists? (closes #5689) [josh@joshpeek.com]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r4650 r4651  
    11*SVN* 
     2 
     3* Added support for conditions on Base.exists? #5689 [josh@joshpeek.com]. Examples: 
     4 
     5    assert (Topic.exists?(:author_name => "David"))  
     6          assert (Topic.exists?(:author_name => "Mary", :approved => true))  
     7          assert (Topic.exists?(["parent_id = ?", 1])) 
    28 
    39* Schema dumper quotes date :default values. [Dave Thomas] 
  • trunk/activerecord/lib/active_record/base.rb

    r4645 r4651  
    424424 
    425425      # Returns true if the given +id+ represents the primary key of a record in the database, false otherwise. 
     426      # You can also pass a set of SQL conditions.  
    426427      # Example: 
    427428      #   Person.exists?(5) 
    428       def exists?(id) 
    429         !find(:first, :conditions => ["#{primary_key} = ?", id]).nil? rescue false 
     429      #   Person.exists?(:name => "David") 
     430      def exists?(conditions) 
     431        conditions = ["#{primary_key} = ?", conditions] if conditions.is_a?(Fixnum) 
     432        !find(:first, :conditions => conditions).nil? rescue false 
    430433      end 
    431434 
  • trunk/activerecord/test/finder_test.rb

    r4586 r4651  
    2222  def test_exists 
    2323    assert (Topic.exists?(1)) 
     24    assert (Topic.exists?(:author_name => "David")) 
     25    assert (Topic.exists?(:author_name => "Mary", :approved => true)) 
     26    assert (Topic.exists?(["parent_id = ?", 1])) 
    2427    assert !(Topic.exists?(45)) 
    2528    assert !(Topic.exists?("foo"))