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

发布于 2024-12-23 00:51:20 字数 196 浏览 2 评论 0原文

我对 Ruby 和 Rails 都是新手。

我正在使用 AASM 将状态机行为放入模型类中。根据新旧状态,我想以不同的方式处理状态更改事件。

如何以“to”和“from”状态作为参数调用“after”回调,或者访问 AASM 持有的内部字段(如果这些字段有我需要的内容)? ...或者我只需要添加之前和之后的回调并将之前的状态保存在之前的回调中?

I am new to both Ruby and Rails.

I'm using AASM to put state machine behavior into a model class. 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? ...or do I just have to add both before and after callbacks and save the previous state in the before callback?

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

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

发布评论

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

评论(1

春花秋月 2024-12-30 00:51:20

您可以访问 ModelClass.aasm_state()

如果您对不同的状态有不同的转换,

。 Aasm 有很好的文档记录。您可以在 aasm 的自述文件中找到说明:https://github.com/rubyist/aasm

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

def make_happy
  # do something
end

...

You can access ModelClass.aasm_state()

If you have different transitions for different states.

Aasm is pretty well documented. You can find the instructions in the README for aasm: https://github.com/rubyist/aasm

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

def make_happy
  # do something
end

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