1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

The type_cast_calculated_value method will trust DB types before casting to a BigDecimal.

[#6365 state:committed]

Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
This commit is contained in:
Ken Collins 2011-02-03 15:07:03 -05:00 committed by Santiago Pastorino
parent 092a4e296d
commit 95d5d9b6c4
2 changed files with 7 additions and 1 deletions

View file

@ -285,7 +285,7 @@ module ActiveRecord
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

View file

@ -28,6 +28,12 @@ class CalculationsTest < ActiveRecord::TestCase
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