开发者

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

开发者 https://www.devze.com 2023-03-16 15:14 出处:网络
given a model like: class SentenceItem < ActiveRecord::Base after_update :send_changes def send_changes

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号