Rails, is there a way in the model to provide diff since the last update?

for a model, for example:

class SentenceItem < ActiveRecord::Base

  after_update :send_changes

  def send_changes
     #### Is it possible to do a diff here with dirty/changed? Showing what changed since the last save?
  end

end

And that sentence module has a text box.

Is it possible to make diff here with dirty / modified? Shows what has changed since the last save?

thank

+5
source share
1 answer

Yes, there is a way. From an ActiveModel :: Dirty document :

A newly created object does not change:

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

What attributes have changed?

person.name = 'Bob'
person.changed        # => ['name']
person.changes        # => { 'name' => ['Bill', 'Bob'] }
+6
source

All Articles