From 3f2c86573155be1143107a8d81154b119fcb08ed Mon Sep 17 00:00:00 2001 From: Alberto Almagro Date: Fri, 4 Jan 2019 00:25:05 +0100 Subject: [PATCH 1/2] Revert "Fix NumericData.average test on ruby 2.6" This reverts commit 89b4612ffc97e6648f5cf807906ae210e05acdda. --- activerecord/test/cases/calculations_test.rb | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index 5b5202d167..ade6b9e832 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -57,12 +57,8 @@ def test_should_return_integer_average_if_db_returns_such assert_equal 3, value end - def test_should_return_nil_to_d_as_average - if nil.respond_to?(:to_d) - assert_equal BigDecimal(0), NumericData.average(:bank_balance) - else - assert_nil NumericData.average(:bank_balance) - end + def test_should_return_nil_as_average + assert_nil NumericData.average(:bank_balance) end def test_should_get_maximum_of_field From d237c7c72ccfd87302983669f83c8c90d2aec82e Mon Sep 17 00:00:00 2001 From: Alberto Almagro Date: Fri, 4 Jan 2019 00:27:12 +0100 Subject: [PATCH 2/2] Make average compatible accross Ruby versions Since Ruby 2.6.0 NilClass#to_d is returning `BigDecimal` 0.0, this breaks `average` compatibility with prior Ruby versions. This patch makes `average` return `nil` in all Ruby versions when there are no rows. --- activerecord/lib/active_record/relation/calculations.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index 3ef6e7928f..c2c4a5a882 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -401,7 +401,7 @@ def type_cast_calculated_value(value, type, operation = nil) case operation when "count" then value.to_i when "sum" then type.deserialize(value || 0) - when "average" then value.respond_to?(:to_d) ? value.to_d : value + when "average" then value&.respond_to?(:to_d) ? value.to_d : value else type.deserialize(value) end end