Skip to content

Instantly share code, notes, and snippets.

@dburger
Created June 20, 2009 04:14
Show Gist options
  • Save dburger/133044 to your computer and use it in GitHub Desktop.
Save dburger/133044 to your computer and use it in GitHub Desktop.
def field_changed?(attr, old, value)
if column = column_for_attribute(attr)
if column.type == :integer && column.null && (old.nil? || old == 0)
# 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?
# else no!!!!!!!!!!! not the else branch
# value = column.type_cast(value)
end
value = column.type_cast(value)
end
old != value
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment