mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Merge pull request #1979 from bogdan/association_sum_array_compatibility
Fixed AR::Relation#sum compatibility with Array#sum
This commit is contained in:
@@ -89,8 +89,12 @@ module ActiveRecord
|
||||
# +calculate+ for examples with options.
|
||||
#
|
||||
# Person.sum('age') # => 4562
|
||||
def sum(column_name, options = {})
|
||||
calculate(:sum, column_name, options)
|
||||
def sum(*args)
|
||||
if block_given?
|
||||
self.to_a.sum(*args) {|*block_args| yield(*block_args)}
|
||||
else
|
||||
calculate(:sum, *args)
|
||||
end
|
||||
end
|
||||
|
||||
# This calculates aggregate values in the given column. Methods for count, sum, average,
|
||||
|
||||
@@ -397,6 +397,10 @@ class CalculationsTest < ActiveRecord::TestCase
|
||||
Account.sum(:credit_limit, :from => 'accounts', :conditions => "credit_limit > 50")
|
||||
end
|
||||
|
||||
def test_sum_array_compatibility
|
||||
assert_equal Account.sum(:credit_limit), Account.sum(&:credit_limit)
|
||||
end
|
||||
|
||||
def test_average_with_from_option
|
||||
assert_equal Account.average(:credit_limit), Account.average(:credit_limit, :from => 'accounts')
|
||||
assert_equal Account.average(:credit_limit, :conditions => "credit_limit > 50"),
|
||||
|
||||
Reference in New Issue
Block a user