提交 091e6f79 编写于 作者: B Ben Symonds 提交者: Jeremy Kemper

Change field_changed? method to handle the case where a nullable integer...

Change field_changed? method to handle the case where a nullable integer column is changed from 0 to '0'

[#1530 state:committed]
Signed-off-by: NJeremy Kemper <jeremy@bitsweat.net>
上级 ebec9d43
......@@ -151,12 +151,12 @@ def update_with_dirty
def field_changed?(attr, old, value)
if column = column_for_attribute(attr)
if column.type == :integer && column.null && (old.nil? || old == 0)
if column.type == :integer && column.null && (old.nil? || old == 0) && value.blank?
# For nullable integer columns, NULL gets stored in database for blank (i.e. '') values.
# Hence we don't record it as a change if the value changes from nil to ''.
# If an old value of 0 is set to '' we want this to get changed to nil as otherwise it'll
# be typecast back to 0 (''.to_i => 0)
value = nil if value.blank?
value = nil
else
value = column.type_cast(value)
end
......
......@@ -68,6 +68,18 @@ def test_nullable_integer_not_marked_as_changed_if_new_value_is_blank
end
end
def test_nullable_integer_zero_to_string_zero_not_marked_as_changed
pirate = Pirate.new
pirate.parrot_id = 0
pirate.catchphrase = 'arrr'
assert pirate.save!
assert !pirate.changed?
pirate.parrot_id = '0'
assert !pirate.changed?
end
def test_zero_to_blank_marked_as_changed
pirate = Pirate.new
pirate.catchphrase = "Yarrrr, me hearties"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册