public this repo is viewable by everyone
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
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
technoweenie (author)
about 1 month ago
commit  78c2d9fc223e7a9945aee65c838f7ce78e9ddb3e
tree    a1e951f726d0831a75289d29396b7f7feb34f097
parent  4d594cffcfc93b37fad4e423ec8593299e50133c
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *SVN*
0
 
0
+* ActiveRecord::Base#sum defaults to 0 if no rows are returned. Closes #11550 [kamal]
0
+
0
 * Ensure that respond_to? considers dynamic finder methods. Closes #11538. [floehopper]
0
 
0
 * Ensure that save on parent object fails for invalid has_one association. Closes #10518. [Pratik]
...
71
72
73
74
 
75
76
77
...
71
72
73
 
74
75
76
77
0
@@ -71,7 +71,7 @@ module ActiveRecord
0
       #
0
       # Person.sum('age')
0
       def sum(column_name, options = {})
0
- calculate(:sum, column_name, options)
0
+ calculate(:sum, column_name, options) || 0
0
       end
0
 
0
       # This calculates aggregate values in the given column. Methods for count, sum, average, minimum, and maximum have been added as shortcuts.
...
97
98
99
 
 
 
 
100
101
102
...
97
98
99
100
101
102
103
104
105
106
0
@@ -97,6 +97,10 @@ class CalculationsTest < ActiveRecord::TestCase
0
     assert_equal 105, Account.sum(:credit_limit, :conditions => 'firm_id = 6')
0
   end
0
 
0
+ def test_should_return_zero_if_sum_conditions_return_nothing
0
+ assert_equal 0, Account.sum(:credit_limit, :conditions => '1 = 2')
0
+ end
0
+
0
   def test_should_group_by_summed_field_with_conditions
0
     c = Account.sum(:credit_limit, :conditions => 'firm_id > 1',
0
                                    :group => :firm_id)

Comments

    No one has commented yet.