mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge branch 'master' of git://github.com/rails/rails
This commit is contained in:
commit
f32e3aff5f
3 changed files with 38 additions and 44 deletions
|
@ -64,41 +64,7 @@ module ActiveRecord
|
||||||
0
|
0
|
||||||
end
|
end
|
||||||
|
|
||||||
# Calculates the average value on a given column. The value is returned as
|
delegate :average, :minimum, :maximum, :sum, :to => :scoped
|
||||||
# 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 = {})
|
|
||||||
calculate(:average, column_name, options)
|
|
||||||
end
|
|
||||||
|
|
||||||
# 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 = {})
|
|
||||||
calculate(:minimum, column_name, options)
|
|
||||||
end
|
|
||||||
|
|
||||||
# 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 = {})
|
|
||||||
calculate(:maximum, column_name, options)
|
|
||||||
end
|
|
||||||
|
|
||||||
# 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 = {})
|
|
||||||
calculate(:sum, column_name, options)
|
|
||||||
end
|
|
||||||
|
|
||||||
# This calculates aggregate values in the given column. Methods for count, sum, average, minimum, and maximum have been added as shortcuts.
|
# This calculates aggregate values in the given column. Methods for count, sum, average, minimum, and maximum have been added as shortcuts.
|
||||||
# Options such as <tt>:conditions</tt>, <tt>:order</tt>, <tt>:group</tt>, <tt>:having</tt>, and <tt>:joins</tt> can be passed to customize the query.
|
# Options such as <tt>:conditions</tt>, <tt>:order</tt>, <tt>:group</tt>, <tt>:having</tt>, and <tt>:joins</tt> can be passed to customize the query.
|
||||||
|
|
|
@ -182,7 +182,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def ==(other)
|
def ==(other)
|
||||||
to_a == other.to_a
|
other.respond_to?(:to_a) ? to_a == other.to_a : false
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -5,20 +5,40 @@ module ActiveRecord
|
||||||
calculate(:count, *construct_count_options_from_args(*args))
|
calculate(:count, *construct_count_options_from_args(*args))
|
||||||
end
|
end
|
||||||
|
|
||||||
def average(column_name)
|
# Calculates the average value on a given column. The value is returned as
|
||||||
calculate(:average, column_name)
|
# 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
|
end
|
||||||
|
|
||||||
def minimum(column_name)
|
# Calculates the minimum value on a given column. The value is returned
|
||||||
calculate(:minimum, column_name)
|
# 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
|
end
|
||||||
|
|
||||||
def maximum(column_name)
|
# Calculates the maximum value on a given column. The value is returned
|
||||||
calculate(:maximum, column_name)
|
# 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
|
end
|
||||||
|
|
||||||
def sum(column_name)
|
# Calculates the sum of values on a given column. The value is returned
|
||||||
calculate(:sum, column_name)
|
# 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
|
end
|
||||||
|
|
||||||
def calculate(operation, column_name, options = {})
|
def calculate(operation, column_name, options = {})
|
||||||
|
@ -49,6 +69,14 @@ module ActiveRecord
|
||||||
0
|
0
|
||||||
end
|
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
|
private
|
||||||
|
|
||||||
def execute_simple_calculation(operation, column_name, distinct) #:nodoc:
|
def execute_simple_calculation(operation, column_name, distinct) #:nodoc:
|
||||||
|
|
Loading…
Reference in a new issue