Skip to content

Commit

Permalink
Add ActiveRecord::Base.exists? with no args [rails#1817 state:committed]
Browse files Browse the repository at this point in the history
Signed-off-by: David Heinemeier Hansson <david@loudthinking.com>
  • Loading branch information
smtlaissezfaire authored and dhh committed Feb 5, 2009
1 parent 6db78e8 commit 5a8f764
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
5 changes: 5 additions & 0 deletions activerecord/CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
*Edge*

* Added that ActiveRecord::Base.exists? can be called with no arguments #1817 [Scott Taylor]


*2.3.0 [RC1] (February 1st, 2009)*

* Add Support for updating deeply nested models from a single form. #1202 [Eloy Duran]
Expand Down
6 changes: 4 additions & 2 deletions activerecord/lib/active_record/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ def find_by_sql(sql)


# Returns true if a record exists in the table that matches the +id+ or
# conditions given, or false otherwise. The argument can take four forms:
# conditions given, or false otherwise. The argument can take five forms:
#
# * Integer - Finds the record with this primary key.
# * String - Finds the record with a primary key corresponding to this
Expand All @@ -672,6 +672,7 @@ def find_by_sql(sql)
# (such as <tt>['color = ?', 'red']</tt>).
# * Hash - Finds the record that matches these +find+-style conditions
# (such as <tt>{:color => 'red'}</tt>).
# * No args - Returns false if the table is empty, true otherwise.
#
# For more information about specifying conditions as a Hash or Array,
# see the Conditions section in the introduction to ActiveRecord::Base.
Expand All @@ -685,7 +686,8 @@ def find_by_sql(sql)
# Person.exists?('5')
# Person.exists?(:name => "David")
# Person.exists?(['name LIKE ?', "%#{query}%"])
def exists?(id_or_conditions)
# Person.exists?
def exists?(id_or_conditions = {})
connection.select_all(
construct_finder_sql(
:select => "#{quoted_table_name}.#{primary_key}",
Expand Down
11 changes: 10 additions & 1 deletion activerecord/test/cases/finder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,16 @@ def test_exists

assert_raise(NoMethodError) { Topic.exists?([1,2]) }
end


def test_exists_returns_true_with_one_record_and_no_args
assert Topic.exists?
end

def test_does_not_exist_with_empty_table_and_no_args_given
Topic.delete_all
assert !Topic.exists?
end

def test_exists_with_aggregate_having_three_mappings
existing_address = customers(:david).address
assert Customer.exists?(:address => existing_address)
Expand Down

0 comments on commit 5a8f764

Please sign in to comment.