Skip to content

Commit 91ddb30

Browse files
committed
Do not allow passing the column name to sum when a block is passed
1 parent 67356f2 commit 91ddb30

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

activerecord/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Do not allow passing the column name to `sum` when a block is passed.
2+
3+
*Rafael Mendonça França*
4+
15
* Do not allow passing the column name to `count` when a block is passed.
26

37
*Rafael Mendonça França*

activerecord/lib/active_record/relation/calculations.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,13 @@ def maximum(column_name)
8484
def sum(column_name = nil)
8585
if block_given?
8686
unless column_name.nil?
87-
ActiveSupport::Deprecation.warn \
88-
"When `sum' is called with a block, it ignores other arguments. " \
89-
"This behavior is now deprecated and will result in an ArgumentError in Rails 6.0."
87+
raise ArgumentError, "Column name argument is not supported when a block is passed."
9088
end
9189

92-
return super()
90+
super()
91+
else
92+
calculate(:sum, column_name)
9393
end
94-
95-
calculate(:sum, column_name)
9694
end
9795

9896
# This calculates aggregate values in the given column. Methods for #count, #sum, #average,

activerecord/test/cases/calculations_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -923,9 +923,9 @@ def test_count_with_block_and_column_name_raises_an_error
923923
end
924924
end
925925

926-
def test_deprecate_sum_with_block_and_column_name
927-
assert_deprecated do
928-
assert_equal 6, Account.sum(:firm_id) { 1 }
926+
def test_sum_with_block_and_column_name_raises_an_error
927+
assert_raises(ArgumentError) do
928+
Account.sum(:firm_id) { 1 }
929929
end
930930
end
931931

0 commit comments

Comments
 (0)