diff options
author | Rick Olson <technoweenie@gmail.com> | 2007-07-17 20:16:35 +0000 |
---|---|---|
committer | Rick Olson <technoweenie@gmail.com> | 2007-07-17 20:16:35 +0000 |
commit | a41305640722f1328ff51ce61c98371d4b648213 (patch) | |
tree | 8a3f8e482a2cc72b17d0937833a9803255e9343d /activerecord/lib | |
parent | b549366216f6cb607bf06c19701912a2e0d1f77c (diff) | |
download | rails-a41305640722f1328ff51ce61c98371d4b648213.tar.gz rails-a41305640722f1328ff51ce61c98371d4b648213.tar.bz2 rails-a41305640722f1328ff51ce61c98371d4b648213.zip |
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
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/calculations.rb | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb index 0a71c92c01..b2d6cef3bc 100644 --- a/activerecord/lib/active_record/calculations.rb +++ b/activerecord/lib/active_record/calculations.rb @@ -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 |