given a model like:
class SentenceItem < ActiveRecord::Base
  after_update :send_changes
  def send_changes
     #### Is it possible to do a diff here with dirty/changed? Showing what's changed since the last save?
  end
end
And that the sentence modle has a text field.
Is it possible to do a diff here with dirty/changed? Showing what's changed since the last save?
Thanks
Yes, there is a way. From the ActiveModel::Dirty documentation:
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']
Which attributes have changed?
person.name = 'Bob'
person.changed        # => ['name']
person.changes        # => { 'name' => ['Bill', 'Bob'] }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With