ActiveRecord 提供更改跟踪,其中调用#name_changed?
返回 true/false 取决于是否name
属性在模型加载时和现在之间发生了变化。
协会有类似的东西吗?我专门寻找 has_many 关联,但所有关联类型都会有用。
Rails 5.2、Ruby 2.5.1
好吧,不是答案,但这是我想出的解决方法。
在我的项目中我已经使用了 gemaudited https://github.com/collectiveidea/audited为了跟踪模型的更改,我也想接收更改。就我而言,模型是Location
我想检查位置是否更改的模型是Employee
In the after_save
然后,我检查是否对该位置进行了审核以及是否在几秒钟内创建了该位置。如果是这样,我可以检查更改。
简化示例:
# app/models/location.rb
class Location < ApplicationRecord
audited
end
# app/models/employee.rb
class Employee < ApplicationRecord
after_save :inform_about_changes!
belongs_to :location
def inform_about_changes!
last_location_change = location.audits.last
if last_location_change.created_at.between?(2.seconds.ago, Time.now)
if last_location_change.audited_changes.include? 'city'
city_was = last_location_change.audited_changes[0]
end
end
end
end
再说一次,这不是一个答案,但它在我的案例中完成了工作
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)