Track unsaved attribute changes.

A newly instantiated object is unchanged:

  person = Person.find_by_name('uncle bob')
  person.changed?       # => false

Change the name:

  person.name = 'Bob'
  person.changed?       # => true
  person.name_changed?  # => true
  person.name_was       # => 'uncle bob'
  person.name_change    # => ['uncle bob', 'Bob']
  person.name = 'Bill'
  person.name_change    # => ['uncle bob', 'Bill']

Save the changes:

  person.save
  person.changed?       # => false
  person.name_changed?  # => false

Assigning the same value leaves the attribute unchanged:

  person.name = 'Bill'
  person.name_changed?  # => false
  person.name_change    # => nil

Which attributes have changed?

  person.name = 'bob'
  person.changed        # => ['name']
  person.changes        # => { 'name' => ['Bill', 'bob'] }

Before modifying an attribute in-place:

  person.name_will_change!
  person.name << 'by'
  person.name_change    # => ['uncle bob', 'uncle bobby']
Methods
Classes and Modules
Module ActiveRecord::Dirty::ClassMethods
Constants
DIRTY_SUFFIXES = ['_changed?', '_change', '_will_change!', '_was']
Public Class methods
included(base)
    # File activerecord/lib/active_record/dirty.rb, line 39
39:     def self.included(base)
40:       base.attribute_method_suffix *DIRTY_SUFFIXES
41:       base.alias_method_chain :write_attribute, :dirty
42:       base.alias_method_chain :save,            :dirty
43:       base.alias_method_chain :save!,           :dirty
44:       base.alias_method_chain :update,          :dirty
45:       base.alias_method_chain :reload,          :dirty
46: 
47:       base.class_attribute :partial_updates
48:       base.partial_updates = true
49: 
50:       base.send(:extend, ClassMethods)
51:     end
Public Instance methods
changed()

List of attributes with unsaved changes.

  person.changed # => []
  person.name = 'bob'
  person.changed # => ['name']
    # File activerecord/lib/active_record/dirty.rb, line 65
65:     def changed
66:       changed_attributes.keys
67:     end
changed?()

Do any attributes have unsaved changes?

  person.changed? # => false
  person.name = 'bob'
  person.changed? # => true
    # File activerecord/lib/active_record/dirty.rb, line 57
57:     def changed?
58:       !changed_attributes.empty?
59:     end
changes()

Map of changed attrs => [original value, new value].

  person.changes # => {}
  person.name = 'bob'
  person.changes # => { 'name' => ['bill', 'bob'] }
    # File activerecord/lib/active_record/dirty.rb, line 73
73:     def changes
74:       changed.inject({}) { |h, attr| h[attr] = attribute_change(attr); h }
75:     end