aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorKen Collins <ken@metaskills.net>2008-11-13 10:45:57 -0600
committerJoshua Peek <josh@joshpeek.com>2008-11-13 10:45:57 -0600
commit57d795bad43d4a3e5eef7151099a8e40808a1031 (patch)
tree34c8b4d27c874c84eb1a5b0e24b8eb21485b425f /activerecord
parentf857da4faf4765ad1579818d2de55f0fabb1b527 (diff)
downloadrails-57d795bad43d4a3e5eef7151099a8e40808a1031.tar.gz
rails-57d795bad43d4a3e5eef7151099a8e40808a1031.tar.bz2
rails-57d795bad43d4a3e5eef7151099a8e40808a1031.zip
Make sure any Fixnum returned by a DB sum is type cast to a Float before standard converstion to a BigDecimal [#8994 state:resolved]
Signed-off-by: Joshua Peek <josh@joshpeek.com>
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/calculations.rb2
-rw-r--r--activerecord/test/cases/calculations_test.rb5
2 files changed, 6 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb
index 6f4e02b430..65512d534a 100644
--- a/activerecord/lib/active_record/calculations.rb
+++ b/activerecord/lib/active_record/calculations.rb
@@ -286,7 +286,7 @@ module ActiveRecord
case operation
when 'count' then value.to_i
when 'sum' then type_cast_using_column(value || '0', column)
- when 'avg' then value && (value == 0 ? 0.0.to_d : 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
end
diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb
index 0fa61500c0..8bd0dd0f6e 100644
--- a/activerecord/test/cases/calculations_test.rb
+++ b/activerecord/test/cases/calculations_test.rb
@@ -25,6 +25,11 @@ class CalculationsTest < ActiveRecord::TestCase
def test_should_return_nil_as_average
assert_nil NumericData.average(:bank_balance)
end
+
+ def test_type_cast_calculated_value_should_convert_db_averages_of_fixnum_class_to_decimal
+ assert_equal 0, NumericData.send(:type_cast_calculated_value, 0, nil, 'avg')
+ assert_equal 53.0, NumericData.send(:type_cast_calculated_value, 53, nil, 'avg')
+ end
def test_should_get_maximum_of_field
assert_equal 60, Account.maximum(:credit_limit)