aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorJonathan Viney <jonathan@jonathan-desktop.(none)>2008-06-02 15:00:15 +1200
committerPratik Naik <pratiknaik@gmail.com>2008-06-02 12:50:44 +0100
commit4210d85a3f3ce4e980f473e9ed2becb84b58363b (patch)
tree843c995a2c5852d03b7fbd0634424420d49a670f /activerecord
parenta980eb8c7734f14109d8c2a02a88dafdf682e0dc (diff)
downloadrails-4210d85a3f3ce4e980f473e9ed2becb84b58363b.tar.gz
rails-4210d85a3f3ce4e980f473e9ed2becb84b58363b.tar.bz2
rails-4210d85a3f3ce4e980f473e9ed2becb84b58363b.zip
Ensure Associations#sum returns 0 when no rows are returned. [#295 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/calculations.rb6
-rw-r--r--activerecord/test/cases/calculations_test.rb3
2 files changed, 5 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb
index 10e8330d1c..889b7845fb 100644
--- a/activerecord/lib/active_record/calculations.rb
+++ b/activerecord/lib/active_record/calculations.rb
@@ -71,7 +71,7 @@ module ActiveRecord
#
# Person.sum('age')
def sum(column_name, options = {})
- calculate(:sum, column_name, options) || 0
+ calculate(:sum, column_name, options)
end
# This calculates aggregate values in the given column. Methods for count, sum, average, minimum, and maximum have been added as shortcuts.
@@ -265,8 +265,8 @@ module ActiveRecord
def type_cast_calculated_value(value, column, operation = nil)
operation = operation.to_s.downcase
case operation
- when 'count' then value.to_i
- when 'avg' then value && value.to_f
+ when 'count', 'sum' then value.to_i
+ when 'avg' then value && value.to_f
else column ? column.type_cast(value) : value
end
end
diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb
index a87fc26905..5cf655f7b7 100644
--- a/activerecord/test/cases/calculations_test.rb
+++ b/activerecord/test/cases/calculations_test.rb
@@ -99,6 +99,7 @@ class CalculationsTest < ActiveRecord::TestCase
def test_should_return_zero_if_sum_conditions_return_nothing
assert_equal 0, Account.sum(:credit_limit, :conditions => '1 = 2')
+ assert_equal 0, companies(:rails_core).companies.sum(:id, :conditions => '1 = 2')
end
def test_should_group_by_summed_field_with_conditions
@@ -266,6 +267,6 @@ class CalculationsTest < ActiveRecord::TestCase
end
def test_should_sum_expression
- assert_equal "636", Account.sum("2 * credit_limit")
+ assert_equal 636, Account.sum("2 * credit_limit")
end
end