gpt4 book ai didi

ruby-on-rails - AASM 回调不起作用

转载 作者:行者123 更新时间:2023-12-04 06:22:01 24 4
gpt4 key购买 nike

我正在使用 AASM 来管理状态,并且只是想要一些列来跟踪状态更改的时间,但回调似乎不起作用。当然,问题可能出在我的方法上,我只是不确定。

        aasm_state :active, :after => :activate
aasm_state :inactive
aasm_state :deactivated, :after => :deactivate

aasm_event :active do
transitions :to => :active, :from => [:inactive]
transitions :to => :active, :from => [:deactivated]
end

aasm_event :deactivated do
transitions :to => :deactivated, :from => [:active]
end

def activate
activated_at = Time.now
end

def deactivate
deactivated_at = Time.now
end

最佳答案

在 Rails 3 版本的 AASM 中,您需要将回调应用于转换事件,而不是状态。所以你的代码可以是 -

    aasm_state :active

aasm_event :active, :after => :activate do

transitions :to => :active, :from => [:inactive]
transitions :to => :active, :from => [:deactivated]
end

关于ruby-on-rails - AASM 回调不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5998528/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com