Skip to content

Commit ad9983f

Browse files
Deprecate Relation#sum with a block.
To perform a sum calculation over the array of elements, use to_a.sum(&block). Please check the discussion in f9cb645 for more context.
1 parent 76a6bfd commit ad9983f

File tree

5 files changed

+22
-3
lines changed

5 files changed

+22
-3
lines changed

activerecord/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
## Rails 4.0.0 (unreleased) ##
22

3+
* Deprecate calling `Relation#sum` with a block. To perform a calculation over
4+
the array result of the relation, use `to_a.sum(&block)`.
5+
6+
*Carlos Antonio da Silva*
7+
38
* Fix postgresql adapter to handle BC timestamps correctly
49

510
HistoryEvent.create!(:name => "something", :occured_at => Date.new(0) - 5.years)

activerecord/lib/active_record/associations.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def association_instance_set(name, association)
234234
# others.size | X | X | X
235235
# others.length | X | X | X
236236
# others.count | X | X | X
237-
# others.sum(args*,&block) | X | X | X
237+
# others.sum(*args) | X | X | X
238238
# others.empty? | X | X | X
239239
# others.clear | X | X | X
240240
# others.delete(other,other,...) | X | X | X

activerecord/lib/active_record/relation/calculations.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'active_support/deprecation'
2+
13
module ActiveRecord
24
module Calculations
35
# Count the records.
@@ -51,6 +53,10 @@ def maximum(column_name, options = {})
5153
# Person.sum('age') # => 4562
5254
def sum(*args)
5355
if block_given?
56+
ActiveSupport::Deprecation.warn(
57+
"Calling #sum with a block is deprecated and will be removed in Rails 4.1. " \
58+
"If you want to perform sum calculation over the array of elements, use `to_a.sum(&block)`."
59+
)
5460
self.to_a.sum(*args) {|*block_args| yield(*block_args)}
5561
else
5662
calculate(:sum, *args)

activerecord/test/cases/associations/has_many_associations_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,6 +1658,12 @@ def test_collection_association_with_private_kernel_method
16581658
assert_deprecated { klass.has_many :foo, :counter_sql => 'lol' }
16591659
end
16601660

1661+
test "sum calculation with block for array compatibility is deprecated" do
1662+
assert_deprecated do
1663+
posts(:welcome).comments.sum { |c| c.id }
1664+
end
1665+
end
1666+
16611667
test "has many associations on new records use null relations" do
16621668
post = Post.new
16631669

activerecord/test/cases/calculations_test.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,10 @@ def test_sum_with_from_option
389389
Account.where("credit_limit > 50").from('accounts').sum(:credit_limit)
390390
end
391391

392-
def test_sum_array_compatibility
393-
assert_equal Account.sum(:credit_limit), Account.sum(&:credit_limit)
392+
def test_sum_array_compatibility_deprecation
393+
assert_deprecated do
394+
assert_equal Account.sum(:credit_limit), Account.sum(&:credit_limit)
395+
end
394396
end
395397

396398
def test_average_with_from_option

0 commit comments

Comments
 (0)