aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorRick Olson <technoweenie@gmail.com>2006-04-14 14:14:19 +0000
committerRick Olson <technoweenie@gmail.com>2006-04-14 14:14:19 +0000
commit32f66f09dbcd44f2edbbacbd23d0a9d0aa60b917 (patch)
tree75a6647160286de9cbd911c897a1803d4dcacaf7 /activerecord/lib/active_record
parentb305ef3ef7499301ca2ac34ee78e0f72273dcade (diff)
downloadrails-32f66f09dbcd44f2edbbacbd23d0a9d0aa60b917.tar.gz
rails-32f66f09dbcd44f2edbbacbd23d0a9d0aa60b917.tar.bz2
rails-32f66f09dbcd44f2edbbacbd23d0a9d0aa60b917.zip
Modify ActiveRecord::Base.count so it shares the same signature as #calculate, but maintains backwards compatibility.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4211 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/calculations.rb23
1 files changed, 13 insertions, 10 deletions
diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb
index d586bec8e6..863aedbf9c 100644
--- a/activerecord/lib/active_record/calculations.rb
+++ b/activerecord/lib/active_record/calculations.rb
@@ -42,26 +42,29 @@ module ActiveRecord
#
# Note: Person.count(:all) will not work because it will use :all as the condition. Use Person.count instead.
def count(*args)
- options = {}
-
- #For backwards compatibility, we need to handle both count(conditions=nil, joins=nil) or count(options={}).
- if args.size >= 0 and args.size <= 2
+ options = {}
+ column_name = :all
+ # For backwards compatibility, we need to handle both count(conditions=nil, joins=nil) or count(options={}) or count(column_name=:all, options={}).
+ if args.size >= 0 && args.size <= 2
if args.first.is_a?(Hash)
- options = args.first
- #should we verify the options hash???
+ options = args.first
elsif args[1].is_a?(Hash)
+ options = args[1]
column_name = args.first
- options = args[1]
else
- # Handle legacy paramter options: def count(conditions=nil, joins=nil)
+ # Handle legacy paramter options: def count(conditions=nil, joins=nil)
options.merge!(:conditions => args[0]) if args.length > 0
- options.merge!(:joins => args[1]) if args.length > 1
+ options.merge!(:joins => args[1]) if args.length > 1
end
else
raise(ArgumentError, "Unexpected parameters passed to count(*args): expected either count(conditions=nil, joins=nil) or count(options={})")
end
- (scope(:find, :include) || options[:include]) ? count_with_associations(options) : calculate(:count, :all, options)
+ if options[:include] || scope(:find, :include)
+ count_with_associations(options)
+ else
+ calculate(:count, column_name, options)
+ end
end
# Calculates average value on a given column. The value is returned as a float. See #calculate for examples with options.