1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix and properly document/test count(column_name) usage. Closes #8999 [lifofifo]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7192 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Rick Olson 2007-07-17 20:16:35 +00:00
parent b549366216
commit a413056407
3 changed files with 26 additions and 10 deletions

View file

@ -1,5 +1,7 @@
*SVN*
* Fix and properly document/test count(column_name) usage. Closes #8999 [lifofifo]
* Remove deprecated count(conditions=nil, joins=nil) usage. Closes #8993 [lifofifo]
* Change belongs_to so that the foreign_key assumption is taken from the association name, not the class name. Closes #8992 [hasmanyjosh]

View file

@ -6,12 +6,13 @@ module ActiveRecord
end
module ClassMethods
# Count operates using two different approaches.
# Count operates using three different approaches.
#
# * Count all: By not passing any parameters to count, it will return a count of all the rows for the model.
# * Count using column : By passing a column name to count, it will return a count of all the rows for the model with supplied column present
# * Count using options will find the row count matched by the options used.
#
# The second approach, count using options, accepts an option hash as the only parameter. The options are:
# The third approach, count using options, accepts an option hash as the only parameter. The options are:
#
# * <tt>:conditions</tt>: An SQL fragment like "administrator = 1" or [ "user_name = ?", username ]. See conditions in the intro.
# * <tt>:joins</tt>: An SQL fragment for additional joins like "LEFT JOIN comments ON comments.post_id = id". (Rarely needed).
@ -28,6 +29,9 @@ module ActiveRecord
# Examples for counting all:
# Person.count # returns the total count of all people
#
# Examples for counting by column:
# Person.count(:age) # returns the total count of all people whose age is present in database
#
# Examples for count with options:
# Person.count(:conditions => "age > 26")
# Person.count(:conditions => "age > 26 AND job.salary > 60000", :include => :job) # because of the named association, it finds the DISTINCT count using LEFT OUTER JOIN.
@ -123,19 +127,21 @@ module ActiveRecord
def construct_count_options_from_args(*args)
options = {}
column_name = :all
# We need to handle
# count()
# count(:column_name=:all)
# count(options={})
# count(column_name=:all, options={})
if args[0].is_a?(Hash)
options = args[0]
elsif args[1].is_a?(Hash)
case args.size
when 1
args[0].is_a?(Hash) ? options = args[0] : column_name = args[0]
when 2
column_name, options = args
else
raise ArgumentError, "Unexpected parameters passed to count(options={}): #{args.inspect}"
raise ArgumentError, "Unexpected parameters passed to count(): #{args.inspect}"
end if args.size > 0
[column_name, options]
end

View file

@ -227,12 +227,20 @@ class CalculationsTest < Test::Unit::TestCase
assert_raises(ArgumentError) { Company.send(:validate_calculation_options, :sum, :foo => :bar) }
assert_raises(ArgumentError) { Company.send(:validate_calculation_options, :count, :foo => :bar) }
end
def test_should_count_selected_field_with_include
assert_equal 6, Account.count(:distinct => true, :include => :firm)
assert_equal 4, Account.count(:distinct => true, :include => :firm, :select => :credit_limit)
end
def test_count_with_column_parameter
assert_equal 5, Account.count(:firm_id)
end
def test_count_with_column_and_options_parameter
assert_equal 2, Account.count(:firm_id, :conditions => "credit_limit = 50")
end
def test_count_with_no_parameters_isnt_deprecated
assert_not_deprecated { Account.count }
end