From 8f50cfeae0f208ab35441f50de0af69b8614a88a Mon Sep 17 00:00:00 2001 From: Brian Lopez Date: Fri, 30 Apr 2010 14:59:41 -0700 Subject: [PATCH] Allow pre-casted values (other than nil) to pass through from calculations un-touched --- .../lib/active_record/relation/calculations.rb | 14 +++++++++----- 1 files changed, 9 insertions(+), 5 deletions(-) diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index a5ea6e7..8ab5eaa 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -239,11 +239,15 @@ module ActiveRecord end def type_cast_calculated_value(value, column, operation = nil) - case operation - when 'count' then value.to_i - when 'sum' then type_cast_using_column(value || '0', column) - when 'average' then value && (value.is_a?(Fixnum) ? value.to_f : value).to_d - else type_cast_using_column(value, column) + if value.is_a?(String) || value.nil? + case operation + when 'count' then value.to_i + when 'sum' then type_cast_using_column(value || '0', column) + when 'average' 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