diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2019-01-04 10:32:37 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-01-04 10:32:37 +0900 |
commit | a5a22c4ea142974117d6afbde68a8074617bda0c (patch) | |
tree | 01253c463ade943dd8c8cb65bfcd55c754c0571f /activerecord | |
parent | 912db8bd060c79fbbc9fd73e90b3920dff7bfd48 (diff) | |
parent | d237c7c72ccfd87302983669f83c8c90d2aec82e (diff) | |
download | rails-a5a22c4ea142974117d6afbde68a8074617bda0c.tar.gz rails-a5a22c4ea142974117d6afbde68a8074617bda0c.tar.bz2 rails-a5a22c4ea142974117d6afbde68a8074617bda0c.zip |
Merge pull request #34858 from albertoalmagro/make-rails-compatible-accross-ruby-versions
Make average compatible across ruby versions
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/relation/calculations.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/calculations_test.rb | 8 |
2 files changed, 3 insertions, 7 deletions
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 @@ module ActiveRecord 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 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 @@ class CalculationsTest < ActiveRecord::TestCase 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 |