通过轨道观察员跟踪场变化
我正在尝试找出一种在有人更新对象时记录更改的方法。现在,我的观察者将存储所有当前值,但我不知道如何确定哪些字段发生了更改以及如何将它们存储在我的 feed 数据库中。
为了简单起见,这里有一个示例模型和观察者。我的目标是跟踪哪个字段已更新并将其存储在我的 Feed 数据库中。如果需要的话,我愿意向我的数据库添加新字段。如果您有任何疑问,请告诉我。
模型
# Table name: milestones
#
# id :integer not null, primary key
# name :string(255)
# project_id :integer
# target_date :datetime
观察者跟踪变化
class MilestoneObserver < ActiveRecord::Observer
def after_update(milestone)
f = Feed.new(
:action => milestone.name,
:project_id => milestone.project_id,
:updated_by_id => "Jordan")
f.save
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要了解哪些字段已更改,请执行以下操作:
请参阅此处的好文章:http://ryandaigle.com/articles/2008/3/31/what-s-new-in-edge-rails-dirty-objects
To know which fields have changed, do:
See nice article here: http://ryandaigle.com/articles/2008/3/31/what-s-new-in-edge-rails-dirty-objects