记录 Rails state_machine 中的转换时间

发布于 2024-12-11 09:24:40 字数 566 浏览 2 评论 0原文

我有一个可以执行一系列操作的学生模型。我为最新操作创建了一个 state_machine。

state_machine :last_action, :initial => :get_invited do
     event :enroll do
      transition [:get_invited, :accept, :schedule] => :enroll
    end
end

我创建了一个 StudentObserver 类,它扩展了 ActiveRecord::Observer 来记录最后一个操作的时间,如下所示:

  def after_transition(student, transition)
    student.last_action_at = Time.now
  end

我认为这个函数会记录最后一个操作的时间(无论最后一个操作是什么)。不知怎的,它不起作用,即使我成功地从一个状态转移到另一个状态,我的last_action_at字段也为空。

我做错了什么?如何获取最新交易的时间?

谢谢。

I have a Student model who can perform a series of actions. I create a state_machine for latest action.

state_machine :last_action, :initial => :get_invited do
     event :enroll do
      transition [:get_invited, :accept, :schedule] => :enroll
    end
end

I create a StudentObserver class, which extends ActiveRecord::Observer to record the time of the last action as follow:

  def after_transition(student, transition)
    student.last_action_at = Time.now
  end

I thought this function will record the timing of the last action (no matter what the last action is). Somehow it doesn't work, and I got empty field of last_action_at even if I transit successfully from one to another state.

What did I do incorrectly? How can I capture the timing of the latest transaction?

Thank you.

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

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

发布评论

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

评论(1

稚然 2024-12-18 09:24:40

一个更简单的解决方案是创建一个 on_transition 挂钩并在那里记录时间。

event :xyz do
  transitions :to => :state1, :from => :state0, :on_transition => :method_name
end

def method_name
  #set time here
end

A much simpler solution to this would be to create an on_transition hook and record the time there.

event :xyz do
  transitions :to => :state1, :from => :state0, :on_transition => :method_name
end

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