Skip to content

Commit

Permalink
The type_cast_calculated_value method will trust DB types before cast…
Browse files Browse the repository at this point in the history
…ing to a BigDecimal.

[#6365 state:committed]

Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
  • Loading branch information
metaskills authored and spastorino committed Feb 3, 2011
1 parent 53b0235 commit 068527b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/relation/calculations.rb
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions activerecord/test/cases/calculations_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit 068527b

Please sign in to comment.