aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2008-06-02 20:40:25 +0100
committerPratik Naik <pratiknaik@gmail.com>2008-06-02 20:41:11 +0100
commitbd75a722a2e9f979bfd1b1d89442e4dd6f3e3af7 (patch)
tree7a6f50646a567af3ecca74ad7483b7502bc7ec33 /activerecord
parent952ec79bec313e0001adfc8c86f7970448d32db9 (diff)
downloadrails-bd75a722a2e9f979bfd1b1d89442e4dd6f3e3af7.tar.gz
rails-bd75a722a2e9f979bfd1b1d89442e4dd6f3e3af7.tar.bz2
rails-bd75a722a2e9f979bfd1b1d89442e4dd6f3e3af7.zip
Ensure AR#sum result is typecasted properly
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/calculations.rb5
-rw-r--r--activerecord/test/cases/calculations_test.rb5
2 files changed, 8 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb
index 889b7845fb..caa8c539d5 100644
--- a/activerecord/lib/active_record/calculations.rb
+++ b/activerecord/lib/active_record/calculations.rb
@@ -265,8 +265,9 @@ module ActiveRecord
def type_cast_calculated_value(value, column, operation = nil)
operation = operation.to_s.downcase
case operation
- when 'count', 'sum' then value.to_i
- when 'avg' then value && value.to_f
+ 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
end
end
diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb
index 5cf655f7b7..aefb13ea9a 100644
--- a/activerecord/test/cases/calculations_test.rb
+++ b/activerecord/test/cases/calculations_test.rb
@@ -102,6 +102,11 @@ class CalculationsTest < ActiveRecord::TestCase
assert_equal 0, companies(:rails_core).companies.sum(:id, :conditions => '1 = 2')
end
+ def test_sum_should_return_valid_values_for_decimals
+ NumericData.create(:bank_balance => 19.83)
+ assert_equal 19.83, NumericData.sum(:bank_balance)
+ end
+
def test_should_group_by_summed_field_with_conditions
c = Account.sum(:credit_limit, :conditions => 'firm_id > 1',
:group => :firm_id)