Skip to content

Commit 19869e7

Browse files
Don't mark Float::INFINITY as changed when reassigning it
Co-authored-by: Jonathan Hefner <[email protected]>
1 parent 9c85851 commit 19869e7

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

activemodel/lib/active_model/type/helpers/numeric.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ def equal_nan?(old_value, new_value)
4242
end
4343

4444
def number_to_non_number?(old_value, new_value_before_type_cast)
45-
old_value != nil && non_numeric_string?(new_value_before_type_cast.to_s)
45+
old_value != nil && !new_value_before_type_cast.is_a?(::Numeric) &&
46+
non_numeric_string?(new_value_before_type_cast.to_s)
4647
end
4748

4849
def non_numeric_string?(value)

activerecord/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* Don't mark Float::INFINITY as changed when reassigning it
2+
3+
When saving a record with a float infinite value, it shouldn't mark as changed
4+
5+
*Maicol Bentancor*
6+
17
* Support `RETURNING` clause for MariaDB
28

39
*fatkodima*, *Nikolay Kondratyev*

activerecord/test/cases/adapters/postgresql/numbers_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,20 @@ def test_update
4848
assert_equal new_single, record.single
4949
assert_equal new_double, record.double
5050
end
51+
52+
def test_reassigning_infinity_does_not_mark_record_as_changed
53+
record = PostgresqlNumber.create!(single: Float::INFINITY, double: -Float::INFINITY)
54+
record.reload
55+
record.single = Float::INFINITY
56+
record.double = -Float::INFINITY
57+
assert_not_predicate record, :changed?
58+
end
59+
60+
def test_reassigning_nan_does_not_mark_record_as_changed
61+
record = PostgresqlNumber.create!(single: Float::NAN, double: Float::NAN)
62+
record.reload
63+
record.single = Float::NAN
64+
record.double = Float::NAN
65+
assert_not_predicate record, :changed?
66+
end
5167
end

0 commit comments

Comments
 (0)