通过轨道观察员跟踪场变化

发布于 2024-11-29 14:08:54 字数 702 浏览 3 评论 0 原文

我正在尝试找出一种在有人更新对象时记录更改的方法。现在,我的观察者将存储所有当前值,但我不知道如何确定哪些字段发生了更改以及如何将它们存储在我的 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

I am trying to figure out a way to log the changes when someone updates an object. Right now my observer will store all of the current values but I am lost on how to determine which fields changed and how to store them in my feeds db.

For sake of simplicity here is a sample model and observer. My goal is to track which field was updated and store it in my Feed db. I am open to adding new fields to my db if they are needed. Let me know if you have any questions.

Model

# Table name: milestones
#
#  id            :integer         not null, primary key
#  name          :string(255)
#  project_id    :integer
#  target_date   :datetime

Observer tracking the change

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰沐子 2024-12-06 14:08:54

要了解哪些字段已更改,请执行以下操作:

object.changes

请参阅此处的好文章:http://ryandaigle.com/articles/2008/3/31/what-s-new-in-edge-rails-dirty-objects

To know which fields have changed, do:

object.changes

See nice article here: http://ryandaigle.com/articles/2008/3/31/what-s-new-in-edge-rails-dirty-objects

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文