Skip to content

Instantly share code, notes, and snippets.

@nikz
Created June 30, 2009 02:06
Show Gist options
  • Select an option

  • Save nikz/137941 to your computer and use it in GitHub Desktop.

Select an option

Save nikz/137941 to your computer and use it in GitHub Desktop.
# Wrap write_attribute to remember original attribute value.
def write_attribute_with_dirty(attr, value)
attr = attr.to_s
# The attribute already has an unsaved change.
if changed_attributes.include?(attr)
old = changed_attributes[attr]
changed_attributes.delete(attr) unless field_changed?(attr, old, value)
else
old = clone_attribute_value(:read_attribute, attr)
changed_attributes[attr] = old if field_changed?(attr, old, value)
end
# Carry on.
write_attribute_without_dirty(attr, value)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment