Ruby on Rails latest stable (v7.1.3.2) - 4 notes
  • 1.0.0
  • 1.1.6
  • 1.2.6
  • 2.0.3
  • 2.1.0 (0)
  • 2.2.1 (0)
  • 2.3.8 (0)
  • 3.0.0
  • 3.0.9
  • 3.1.0
  • 3.2.1
  • 3.2.8
  • 3.2.13
  • 4.0.2
  • 4.1.8
  • 4.2.1
  • 4.2.7
  • 4.2.9
  • 5.0.0.1
  • 5.1.7
  • 5.2.3
  • 6.0.0
  • 6.1.3.1
  • 6.1.7.7
  • 7.0.0
  • 7.1.3.2
  • 7.1.3.4
  • What's this?

Module deprecated or moved

This module is deprecated or moved on the latest stable version. The last existing version (v2.3.8) is shown here.

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

Constants

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

Attributes

Show files where this module is defined (1 file)
Register or log in to add new notes.
June 22, 2008
9 thanks

Bang methods also need will_change!

As it says here at the bottom if you do in-place modifications using << you’ll need to call the will_change method!

This also goes for bang methods. So:

person = Person.first
person.name.downcase!
person.save

will not save anything! Save will never be called. To get the name saved you need to do

person = Person.first
person.name_will_change!
person.name.downcase!
person.save

This will save the name.

June 22, 2008
4 thanks

update_on and update_at only set on attribute change

If you call save, and no attributes are “dirty” (changed), then an update query will not happen against the database, and thus updated_at and updated_on will not be set.

You’ll need to modify at least one field to get updated_at and updated_on to set themselves.

August 4, 2008 - (>= v2.1.0)
1 thank

You can turn off dirty objects

If you expierence problems with dirty objects you can turn it off:

ActiveRecord::Base.partial_updates = false
June 25, 2021 - (>= v5.2.3)
0 thanks

Moved from ActiveRecord to ActiveModel

Don’t let the depreciation warning scare you, Dirty lives under ActiveModel as of 3.0.0

apidock.com/rails/ActiveModel