Module

ActiveRecord::Dirty

Inheritance

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']

Classes & Modules

Constants

Name   Description
DIRTY_SUFFIXES = ['_changed?', '_change', '_will_change!', '_was']

Methods

Class

Visibility Signature
public included (base)

Instance

Visibility Signature
public changed ()
public changed? ()
public changes ()

Class Method Detail

included(base)

Instance Method Detail

changed()

List of attributes with unsaved changes.

  person.changed # => []
  person.name = 'bob'
  person.changed # => ['name']

changed?()

Do any attributes have unsaved changes?

  person.changed? # => false
  person.name = 'bob'
  person.changed? # => true

changes()

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

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