diff --git a/activemodel/lib/active_model/type/helpers/numeric.rb b/activemodel/lib/active_model/type/helpers/numeric.rb index 8e989b286f1d3..6b3d2fb6f3f27 100644 --- a/activemodel/lib/active_model/type/helpers/numeric.rb +++ b/activemodel/lib/active_model/type/helpers/numeric.rb @@ -42,7 +42,8 @@ def equal_nan?(old_value, new_value) end def number_to_non_number?(old_value, new_value_before_type_cast) - old_value != nil && non_numeric_string?(new_value_before_type_cast.to_s) + old_value != nil && !new_value_before_type_cast.is_a?(::Numeric) && + non_numeric_string?(new_value_before_type_cast.to_s) end def non_numeric_string?(value) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index b79cadba7cf68..e5677dc814b4a 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,9 @@ +* Don't mark Float::INFINITY as changed when reassigning it + + When saving a record with a float infinite value, it shouldn't mark as changed + + *Maicol Bentancor* + * Support `RETURNING` clause for MariaDB *fatkodima*, *Nikolay Kondratyev* diff --git a/activerecord/test/cases/adapters/postgresql/numbers_test.rb b/activerecord/test/cases/adapters/postgresql/numbers_test.rb index 824d50ac988bf..a1e4a69b072f4 100644 --- a/activerecord/test/cases/adapters/postgresql/numbers_test.rb +++ b/activerecord/test/cases/adapters/postgresql/numbers_test.rb @@ -48,4 +48,20 @@ def test_update assert_equal new_single, record.single assert_equal new_double, record.double end + + def test_reassigning_infinity_does_not_mark_record_as_changed + record = PostgresqlNumber.create!(single: Float::INFINITY, double: -Float::INFINITY) + record.reload + record.single = Float::INFINITY + record.double = -Float::INFINITY + assert_not_predicate record, :changed? + end + + def test_reassigning_nan_does_not_mark_record_as_changed + record = PostgresqlNumber.create!(single: Float::NAN, double: Float::NAN) + record.reload + record.single = Float::NAN + record.double = Float::NAN + assert_not_predicate record, :changed? + end end