@@ -77,11 +77,8 @@ def update(*)
77
77
78
78
def _field_changed? ( attr , old , value )
79
79
if column = column_for_attribute ( attr )
80
- if column . number? && column . null && ( old . nil? || old == 0 ) && value . blank?
81
- # For nullable numeric columns, NULL gets stored in database for blank (i.e. '') values.
82
- # Hence we don't record it as a change if the value changes from nil to ''.
83
- # If an old value of 0 is set to '' we want this to get changed to nil as otherwise it'll
84
- # be typecast back to 0 (''.to_i => 0)
80
+ if column . number? && ( changes_from_nil_to_empty_string? ( column , old , value ) ||
81
+ changes_from_zero_to_string? ( column , old , value ) )
85
82
value = nil
86
83
else
87
84
value = column . type_cast ( value )
@@ -90,6 +87,19 @@ def _field_changed?(attr, old, value)
90
87
91
88
old != value
92
89
end
90
+
91
+ def changes_from_nil_to_empty_string? ( column , old , value )
92
+ # For nullable numeric columns, NULL gets stored in database for blank (i.e. '') values.
93
+ # Hence we don't record it as a change if the value changes from nil to ''.
94
+ # If an old value of 0 is set to '' we want this to get changed to nil as otherwise it'll
95
+ # be typecast back to 0 (''.to_i => 0)
96
+ column . null && ( old . nil? || old == 0 ) && value . blank?
97
+ end
98
+
99
+ def changes_from_zero_to_string? ( column , old , value )
100
+ # For columns with old 0 and value non-empty string
101
+ old == 0 && value . present? && value != '0'
102
+ end
93
103
end
94
104
end
95
105
end
0 commit comments