Skip to content
This repository
Browse code

Modify ActiveRecord::Base.count so it shares the same signature as #c…

…alculate, but maintains backwards compatibility.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4211 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 32f66f09dbcd44f2edbbacbd23d0a9d0aa60b917 1 parent b305ef3
risk danger olson authored April 14, 2006
23  activerecord/lib/active_record/calculations.rb
@@ -42,26 +42,29 @@ module ClassMethods
42 42
       #
43 43
       # Note: Person.count(:all) will not work because it will use :all as the condition.  Use Person.count instead.
44 44
       def count(*args)
45  
-        options    = {}
46  
-        
47  
-        #For backwards compatibility, we need to handle both count(conditions=nil, joins=nil) or count(options={}).
48  
-        if args.size >= 0 and args.size <= 2
  45
+        options     = {}
  46
+        column_name = :all
  47
+        # For backwards compatibility, we need to handle both count(conditions=nil, joins=nil) or count(options={}) or count(column_name=:all, options={}).
  48
+        if args.size >= 0 && args.size <= 2
49 49
           if args.first.is_a?(Hash)
50  
-            options = args.first
51  
-            #should we verify the options hash???
  50
+            options     = args.first
52 51
           elsif args[1].is_a?(Hash)
  52
+            options     = args[1]
53 53
             column_name = args.first
54  
-            options    = args[1]
55 54
           else
56  
-            # Handle legacy paramter options: def count(conditions=nil, joins=nil) 
  55
+            # Handle legacy paramter options: def count(conditions=nil, joins=nil)
57 56
             options.merge!(:conditions => args[0]) if args.length > 0
58  
-            options.merge!(:joins => args[1]) if args.length > 1
  57
+            options.merge!(:joins => args[1])      if args.length > 1
59 58
           end
60 59
         else
61 60
           raise(ArgumentError, "Unexpected parameters passed to count(*args): expected either count(conditions=nil, joins=nil) or count(options={})")
62 61
         end
63 62
 
64  
-        (scope(:find, :include) || options[:include]) ? count_with_associations(options) : calculate(:count, :all, options)
  63
+        if options[:include] || scope(:find, :include)
  64
+          count_with_associations(options)
  65
+        else
  66
+          calculate(:count, column_name, options)
  67
+        end
65 68
       end
66 69
 
67 70
       # Calculates average value on a given column.  The value is returned as a float.  See #calculate for examples with options.

0 notes on commit 32f66f0

Please sign in to comment.
Something went wrong with that request. Please try again.