diff options
author | Alberto Almagro <albertoalmagro@gmail.com> | 2019-01-04 00:27:12 +0100 |
---|---|---|
committer | Alberto Almagro <albertoalmagro@gmail.com> | 2019-01-04 00:41:22 +0100 |
commit | d237c7c72ccfd87302983669f83c8c90d2aec82e (patch) | |
tree | 10f8e59a03c1fa0de8ca86ccbc058fc0727d18d8 /activerecord/lib | |
parent | 3f2c86573155be1143107a8d81154b119fcb08ed (diff) | |
download | rails-d237c7c72ccfd87302983669f83c8c90d2aec82e.tar.gz rails-d237c7c72ccfd87302983669f83c8c90d2aec82e.tar.bz2 rails-d237c7c72ccfd87302983669f83c8c90d2aec82e.zip |
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.
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/relation/calculations.rb | 2 |
1 files changed, 1 insertions, 1 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 |