aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/calculation_methods.rb
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2010-01-19 15:50:47 +0530
committerPratik Naik <pratiknaik@gmail.com>2010-01-19 15:50:47 +0530
commit4148c686ec2582c457adf277dec5b3ff496627c8 (patch)
tree43e2c28a1f1561c157c759adf3a23d8b3f4e99b9 /activerecord/lib/active_record/relation/calculation_methods.rb
parented8501ef16fb2f5e4bd4d987740f5e5f62978400 (diff)
downloadrails-4148c686ec2582c457adf277dec5b3ff496627c8.tar.gz
rails-4148c686ec2582c457adf277dec5b3ff496627c8.tar.bz2
rails-4148c686ec2582c457adf277dec5b3ff496627c8.zip
Delegate :average, :minimum, :maximum, :sum to Relation
Diffstat (limited to 'activerecord/lib/active_record/relation/calculation_methods.rb')
-rw-r--r--activerecord/lib/active_record/relation/calculation_methods.rb44
1 files changed, 36 insertions, 8 deletions
diff --git a/activerecord/lib/active_record/relation/calculation_methods.rb b/activerecord/lib/active_record/relation/calculation_methods.rb
index 91de89e607..7dd6e04db9 100644
--- a/activerecord/lib/active_record/relation/calculation_methods.rb
+++ b/activerecord/lib/active_record/relation/calculation_methods.rb
@@ -5,20 +5,40 @@ module ActiveRecord
calculate(:count, *construct_count_options_from_args(*args))
end
- def average(column_name)
- calculate(:average, column_name)
+ # Calculates the average value on a given column. The value is returned as
+ # a float, or +nil+ if there's no row. See +calculate+ for examples with
+ # options.
+ #
+ # Person.average('age') # => 35.8
+ def average(column_name, options = {})
+ calculation_relation(options).calculate(:average, column_name)
end
- def minimum(column_name)
- calculate(:minimum, column_name)
+ # Calculates the minimum value on a given column. The value is returned
+ # with the same data type of the column, or +nil+ if there's no row. See
+ # +calculate+ for examples with options.
+ #
+ # Person.minimum('age') # => 7
+ def minimum(column_name, options = {})
+ calculation_relation(options).calculate(:minimum, column_name)
end
- def maximum(column_name)
- calculate(:maximum, column_name)
+ # Calculates the maximum value on a given column. The value is returned
+ # with the same data type of the column, or +nil+ if there's no row. See
+ # +calculate+ for examples with options.
+ #
+ # Person.maximum('age') # => 93
+ def maximum(column_name, options = {})
+ calculation_relation(options).calculate(:maximum, column_name)
end
- def sum(column_name)
- calculate(:sum, column_name)
+ # Calculates the sum of values on a given column. The value is returned
+ # with the same data type of the column, 0 if there's no row. See
+ # +calculate+ for examples with options.
+ #
+ # Person.sum('age') # => 4562
+ def sum(column_name, options = {})
+ calculation_relation(options).calculate(:sum, column_name)
end
def calculate(operation, column_name, options = {})
@@ -49,6 +69,14 @@ module ActiveRecord
0
end
+ def calculation_relation(options = {})
+ if options.present?
+ apply_finder_options(options.except(:distinct)).calculation_relation
+ else
+ (eager_loading? || includes_values.present?) ? construct_relation_for_association_calculations : self
+ end
+ end
+
private
def execute_simple_calculation(operation, column_name, distinct) #:nodoc: