Permalink
Browse files

Make sure any Fixnum returned by a DB sum is type cast to a Float bef…

…ore standard converstion to a BigDecimal [#8994 state:resolved]

Signed-off-by: Joshua Peek <josh@joshpeek.com>
  • Loading branch information...
1 parent f857da4 commit 57d795bad43d4a3e5eef7151099a8e40808a1031 @metaskills metaskills committed with josh Nov 13, 2008
Showing with 6 additions and 1 deletion.
  1. +1 −1 activerecord/lib/active_record/calculations.rb
  2. +5 −0 activerecord/test/cases/calculations_test.rb
@@ -286,7 +286,7 @@ 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 '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
@@ -25,6 +25,11 @@ def test_should_average_field
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)

0 comments on commit 57d795b

Please sign in to comment.