ruby-on-rails – Rails,在模型中是否有一种方法可以提供自上次
发布时间:2020-12-17 03:20:23 所属栏目:百科 来源:网络整理
导读:给出一个如下模型: 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? endend 并且句子模型具有文本字段. 是
给出一个如下模型:
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 并且句子模型具有文本字段. 是否有可能在这里使用脏/更改进行差异?显示自上次保存以来发生了哪些变化? 谢谢 解决方法
是的,有一种方法.从
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'] } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |