From db1eb4c2121ef15b31e7fd87dbc044fd5979cf9f Mon Sep 17 00:00:00 2001 From: Johnny Shields <27655+johnnyshields@users.noreply.github.com> Date: Mon, 29 Sep 2025 23:46:05 +0900 Subject: [PATCH 1/2] Correct BSON::Decimal128 handling in normalize_value Correctly use BSON::Decimal128#to_d if it is available in the driver --- lib/mongoid/attributes.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/mongoid/attributes.rb b/lib/mongoid/attributes.rb index 2d069098af..a965bf05a0 100644 --- a/lib/mongoid/attributes.rb +++ b/lib/mongoid/attributes.rb @@ -371,7 +371,12 @@ def lookup_attribute_presence(name, value) # comparison purposes. This is necessary because `BSON::Decimal128` does # not implement `#==` in a way that is compatible with `BigDecimal`. def normalize_value(value) - value.is_a?(BSON::Decimal128) ? BigDecimal(value.to_s) : value + if value.is_a?(BSON::Decimal128) + # BSON::Decimal128#to_d was introduced in driver version 5.0.0 + value.respond_to?(:to_d) ? value.to_d : BigDecimal(value.to_s) + else + value + end end # Determine if the attribute will not change, by comparing the current From 50d06eec1176c70429af20e9f67510c2e913a68d Mon Sep 17 00:00:00 2001 From: Johnny Shields <27655+johnnyshields@users.noreply.github.com> Date: Mon, 29 Sep 2025 23:50:36 +0900 Subject: [PATCH 2/2] Update comment for BSON::Decimal128 to reflect version change --- lib/mongoid/attributes.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mongoid/attributes.rb b/lib/mongoid/attributes.rb index a965bf05a0..caab0502a5 100644 --- a/lib/mongoid/attributes.rb +++ b/lib/mongoid/attributes.rb @@ -372,7 +372,7 @@ def lookup_attribute_presence(name, value) # not implement `#==` in a way that is compatible with `BigDecimal`. def normalize_value(value) if value.is_a?(BSON::Decimal128) - # BSON::Decimal128#to_d was introduced in driver version 5.0.0 + # BSON::Decimal128#to_d was introduced in bson-ruby version 5.0.0 value.respond_to?(:to_d) ? value.to_d : BigDecimal(value.to_s) else value