Permalink
Browse files

The type_cast_calculated_value method will trust DB types before cast…

…ing to a BigDecimal.

[#6365 state:committed]

Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
  • Loading branch information...
1 parent 53b0235 commit 068527baaf9a49862281c4357296262ae73542d0 @metaskills metaskills committed with spastorino Feb 3, 2011
@@ -276,7 +276,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 'average' then value.try(:to_d)
+ when 'average' then value.respond_to?(:to_d) ? value.to_d : value
else type_cast_using_column(value, column)
end
end
@@ -28,6 +28,12 @@ def test_should_return_decimal_average_of_integer_field
assert_equal 3.5, value
end
+ def test_should_return_integer_average_if_db_returns_such
+ Account.connection.stubs :select_value => 3
+ value = Account.average(:id)
+ assert_equal 3, value
+ end
+
def test_should_return_nil_as_average
assert_nil NumericData.average(:bank_balance)
end

0 comments on commit 068527b

Please sign in to comment.