Skip to content

Commit

Permalink
Make sure any Fixnum returned by a DB sum is type cast to a Float bef…
Browse files Browse the repository at this point in the history
…ore standard converstion to a BigDecimal [rails#8994 state:resolved]

Signed-off-by: Joshua Peek <josh@joshpeek.com>
  • Loading branch information
metaskills authored and josh committed Nov 13, 2008
1 parent f857da4 commit 57d795b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/calculations.rb
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions activerecord/test/cases/calculations_test.rb
Expand Up @@ -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)
Expand Down

0 comments on commit 57d795b

Please sign in to comment.