aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorAlberto Almagro <albertoalmagro@gmail.com>2019-01-04 00:27:12 +0100
committerAlberto Almagro <albertoalmagro@gmail.com>2019-01-04 00:41:22 +0100
commitd237c7c72ccfd87302983669f83c8c90d2aec82e (patch)
tree10f8e59a03c1fa0de8ca86ccbc058fc0727d18d8 /activerecord/lib
parent3f2c86573155be1143107a8d81154b119fcb08ed (diff)
downloadrails-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.rb2
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