记录 Rails state_machine 中的转换时间
我有一个可以执行一系列操作的学生模型。我为最新操作创建了一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个更简单的解决方案是创建一个 on_transition 挂钩并在那里记录时间。
A much simpler solution to this would be to create an on_transition hook and record the time there.