简体   繁体   English

如何在Rails的ASA回调中访问旧状态和新状态?

[英]How do I access old and new states in an aasm callback in rails?

I am new to both Ruby and Rails. 我是Ruby和Rails的新手。

I'm using AASM to put state machine behavior into a model class. 我正在使用AASM将状态机行为放入模型类中。 Depending on the old and new states I want to handle the state change event in different ways. 根据新旧状态,我想以不同方式处理状态更改事件。

How do I either invoke the "after" callback with the "to" and "from" states as arguments or access the internal fields of held by AASM if those have what I need? 我如何使用“ to”和“ from”状态作为参数调用“ after”回调,或者如果有需要的话,如何访问ASM持有的内部字段? ...or do I just have to add both before and after callbacks and save the previous state in the before callback? ...还是只需要在回调之前和之后都添加,然后将之前的状态保存在before回调中?

You can access ModelClass.aasm_state() 您可以访问ModelClass.aasm_state()

If you have different transitions for different states. 如果您对不同的状态有不同的转换。

Aasm is pretty well documented. Aasm的文档非常丰富。 You can find the instructions in the README for aasm: https://github.com/rubyist/aasm 您可以在自述文件中找到有关aasm的说明: https : //github.com/rubyist/aasm

state :dating,   :enter => :make_happy,        :exit => :make_depressed

def make_happy
  # do something
end

...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM