diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2008-10-04 20:03:42 +0100 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2008-10-04 20:13:44 +0100 |
commit | 9599948fbcd67c1c2c5fecc2dca148e998479e33 (patch) | |
tree | 919ce7796f1605c3ba6f7edc81afc2bcac4eaad5 /activerecord/lib/active_record | |
parent | 1dfebd4f0d25ecf50ba4e320234523d9ee3f7523 (diff) | |
download | rails-9599948fbcd67c1c2c5fecc2dca148e998479e33.tar.gz rails-9599948fbcd67c1c2c5fecc2dca148e998479e33.tar.bz2 rails-9599948fbcd67c1c2c5fecc2dca148e998479e33.zip |
Ensure Model.sum and Model.avg typecast appropriately. [#1066 state:resolved]
Model.sum delegates typecasting to the column being summed. If that's not feasible, returns a string.
Model.avg always returns big decimal.
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/calculations.rb | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb index 634236e959..5e33cf1bd4 100644 --- a/activerecord/lib/active_record/calculations.rb +++ b/activerecord/lib/active_record/calculations.rb @@ -285,11 +285,15 @@ module ActiveRecord operation = operation.to_s.downcase case operation when 'count' then value.to_i - when 'sum' then value =~ /\./ ? value.to_f : value.to_i - when 'avg' then value && value.to_f - else column ? column.type_cast(value) : value + when 'sum' then type_cast_using_column(value || '0', column) + when 'avg' then value && value.to_d + else type_cast_using_column(value, column) end end + + def type_cast_using_column(value, column) + column ? column.type_cast(value) : value + end end end end |