Skip to content
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 092a4e2 commit 95d5d9b6c48c08f1fba0c77ecbc97b62b2603824 @metaskills metaskills committed with spastorino Feb 3, 2011
View
2 activerecord/lib/active_record/relation/calculations.rb
@@ -285,7 +285,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
View
6 activerecord/test/cases/calculations_test.rb
@@ -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 95d5d9b

Please sign in to comment.
Something went wrong with that request. Please try again.