aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2015-06-19 10:42:57 -0600
committerSean Griffin <sean@thoughtbot.com>2015-06-19 10:44:43 -0600
commit7d14bd3ff553d6aba11a50b43773bc21ae20f31e (patch)
tree8a3f7cd3f5203b948237631adecfdb65618ead48 /activerecord/lib
parent40bdf0dd4c1f4425b9a91c5fc8b570d35dfd4d73 (diff)
downloadrails-7d14bd3ff553d6aba11a50b43773bc21ae20f31e.tar.gz
rails-7d14bd3ff553d6aba11a50b43773bc21ae20f31e.tar.bz2
rails-7d14bd3ff553d6aba11a50b43773bc21ae20f31e.zip
Use `Enumerable#sum` on `ActiveRecord::Relation` when a block is given
This matches our behavior in other cases where useful enumerable methods might have a different definition in `Relation`. Wanting to actually enumerate over the records in this case is completely reasonable, and wanting `.sum` is reasonable for the same reason it is on `Enumerable` in the first place.
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/relation/calculations.rb8
1 files changed, 6 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb
index 7a28a98721..29cc79db1b 100644
--- a/activerecord/lib/active_record/relation/calculations.rb
+++ b/activerecord/lib/active_record/relation/calculations.rb
@@ -70,8 +70,12 @@ module ActiveRecord
# +calculate+ for examples with options.
#
# Person.sum(:age) # => 4562
- def sum(*args)
- calculate(:sum, *args)
+ def sum(*args, &block)
+ if block_given?
+ to_a.sum(&block)
+ else
+ calculate(:sum, *args)
+ end
end
# This calculates aggregate values in the given column. Methods for count, sum, average,