给定一个模型,如:
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
并且句子模型有一个文本字段。
是否可以在此处使用dirty/changed做一个差异?显示自上次保存以来发生了什么变化?
谢谢
最佳答案
是的,有办法。来自 ActiveModel::Dirty文档:
新实例化的对象不变:
person = Person.find_by_name('Uncle Bob')
person.changed? # => false
更改名称:
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']
哪些属性发生了变化?
person.name = 'Bob'
person.changed # => ['name']
person.changes # => { 'name' => ['Bill', 'Bob'] }
关于ruby-on-rails - Rails,在模型中有没有办法提供自上次更新以来的不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6550836/