From 4210d85a3f3ce4e980f473e9ed2becb84b58363b Mon Sep 17 00:00:00 2001 From: Jonathan Viney Date: Mon, 2 Jun 2008 15:00:15 +1200 Subject: Ensure Associations#sum returns 0 when no rows are returned. [#295 state:resolved] Signed-off-by: Pratik Naik --- activerecord/lib/active_record/calculations.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'activerecord/lib/active_record/calculations.rb') 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 -- cgit v1.2.3