表示谁以及何时更改对象状态的最佳方式? (美国航空航天局)

发布于 2024-08-11 05:34:10 字数 584 浏览 12 评论 0原文

现在,我将上次更新模型状态的用户存储在 state_last_updated_by_id 字段中,并将状态上次更新的时间存储在 state_updated_at 字段中。然后我定义这样的方法:

  def published_at
    return unless state == 'published'
    state_updated_at
  end

  def published_by
    return unless state == 'published'
    state_last_updated_by
  end

  def nominated_for_publishing_at
    return unless state == 'under_review'
    state_updated_at
  end

  def nominated_for_publishing_by
    return unless state == 'under_review'
    state_last_updated_by
  end

这显然不会扩展(并且不是一个很好的方法)——正确的方法是什么?

Right now I'm storing the user who last updated the state of my model in the state_last_updated_by_id field and the time the state was last updated in the state_updated_at field. Then I define methods like this:

  def published_at
    return unless state == 'published'
    state_updated_at
  end

  def published_by
    return unless state == 'published'
    state_last_updated_by
  end

  def nominated_for_publishing_at
    return unless state == 'under_review'
    state_updated_at
  end

  def nominated_for_publishing_by
    return unless state == 'under_review'
    state_last_updated_by
  end

This clearly won't scale (and isn't a great approach to begin with) -- what's the right way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

讽刺将军 2024-08-18 05:34:10

您是否研究过 acts_as_audited 插件

设置起来非常简单,并且会在相关表中记录触发记录更新/保存的人的时间戳、更改和用户 ID。使检索最后一个更改状态的用户变得非常简单。它还可以配置为仅在状态更改时触发。

但是,它的可扩展性可能不如您当前的处理方法,因为它会记录模型状态的每次更改。

Have you looked into the acts_as_audited plugin?

It's very simple to set up, and will record the timestamp, changes and the user id of the person who triggers a record update/save in a related table. Making retrieval of the last user to change state very simple. It can also be configured to only trigger when the state is changed.

However it's probably less scalable than your current method of doing things because it will keep a record of every time the state of your model is changed.

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