From ee86d79915ba91937ac5d2d6bd045c4870da1328 Mon Sep 17 00:00:00 2001 From: Brian Lopez Date: Tue, 4 May 2010 12:20:00 -0700 Subject: [PATCH] [backport] Allow pre-casted values (other than nil) to pass through from calculations un-touched --- activerecord/lib/active_record/calculations.rb | 9 ++++++--- 1 files changed, 6 insertions(+), 3 deletions(-) diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb index eb149e8..a44f090 100644 --- a/activerecord/lib/active_record/calculations.rb +++ b/activerecord/lib/active_record/calculations.rb @@ -294,12 +294,15 @@ module ActiveRecord end def type_cast_calculated_value(value, column, operation = nil) - operation = operation.to_s.downcase - case operation + if value.is_a?(String) || value.nil? + case operation.to_s.downcase when 'count' then value.to_i when 'sum' then type_cast_using_column(value || '0', column) - when 'avg' then value && (value.is_a?(Fixnum) ? value.to_f : value).to_d + when 'avg' then value && (value.is_a?(Fixnum) ? value.to_f : value).to_d else type_cast_using_column(value, column) + end + else + value end end -- 1.7.1