Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

ActiveRecord::Base#sum defaults to 0 if no rows are returned. Closes #…

…11550 [kamal]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9243 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 78c2d9fc223e7a9945aee65c838f7ce78e9ddb3e 1 parent 4d594cf
risk danger olson technoweenie authored
2  activerecord/CHANGELOG
View
@@ -1,5 +1,7 @@
*SVN*
+* ActiveRecord::Base#sum defaults to 0 if no rows are returned. Closes #11550 [kamal]
+
* Ensure that respond_to? considers dynamic finder methods. Closes #11538. [floehopper]
* Ensure that save on parent object fails for invalid has_one association. Closes #10518. [Pratik]
2  activerecord/lib/active_record/calculations.rb
View
@@ -71,7 +71,7 @@ def maximum(column_name, options = {})
#
# Person.sum('age')
def sum(column_name, options = {})
- calculate(:sum, column_name, options)
+ calculate(:sum, column_name, options) || 0
end
# This calculates aggregate values in the given column. Methods for count, sum, average, minimum, and maximum have been added as shortcuts.
4 activerecord/test/cases/calculations_test.rb
View
@@ -97,6 +97,10 @@ def test_should_sum_field_with_conditions
assert_equal 105, Account.sum(:credit_limit, :conditions => 'firm_id = 6')
end
+ def test_should_return_zero_if_sum_conditions_return_nothing
+ assert_equal 0, Account.sum(:credit_limit, :conditions => '1 = 2')
+ end
+
def test_should_group_by_summed_field_with_conditions
c = Account.sum(:credit_limit, :conditions => 'firm_id > 1',
:group => :firm_id)
Please sign in to comment.
Something went wrong with that request. Please try again.