铁轨触发嵌套协会自动架上的AASM事件
我有一个父母和子模型模型定义,每个架构都有自己的AASM状态计算机
class Project < ApplicationRecord
has_many :tasks, autosave: true
include AASM
aasm do
state :created, initial: true
state :in_progress
state :completed
event :start do
transitions from: :created, to: :in_progress
end
end
end
class Task < ApplicationRecord
belongs_to :project
include AASM
aasm do
state :open, initial: true
state :in_progress
state :failed
state :reopened
state :closed, after: :notify_task_closed
event :close do
transitions from: :in_progress, to: :closed
end
end
end
,现在我有了一个页面,用户可以在该页面上编辑该项目,并在任务上批量编辑/perform-aasm-actions。现在,当用户提交此类请求时,我会收到已编辑的字段和要在任务上触发的操作(事件)。
由于autosave
是为任务
project 类启用的,我想执行一个保存以
- 更新项目属性更新项目
- 属性更新任务属性
- 触发任务的各自事件。
捕获是在上述每个点运行验证,并在任何步骤失败的情况下还原整个交易。
我遇到以下问题,
- 在自动赛期间实现这一目标的正确模式是什么?
- 何时以及如何触发Autosave期间的AASM事件?
- 如果有任何验证/后卫失败,如何起泡错误?
I have a parent and child model schema defined with each having its own AASM state machine
class Project < ApplicationRecord
has_many :tasks, autosave: true
include AASM
aasm do
state :created, initial: true
state :in_progress
state :completed
event :start do
transitions from: :created, to: :in_progress
end
end
end
class Task < ApplicationRecord
belongs_to :project
include AASM
aasm do
state :open, initial: true
state :in_progress
state :failed
state :reopened
state :closed, after: :notify_task_closed
event :close do
transitions from: :in_progress, to: :closed
end
end
end
Now I have a page where users can edit the project and also edit/perform-aasm-actions in bulk on the tasks. Now when a user submits such request, I receive the edited fields and the actions(events) to be triggered on tasks.
Since autosave
is enabled for the tasks
association for the Project
class, I would like to perform a single save to
- Update project attributes
- Update task attributes
- Trigger respective events on the tasks.
The catch is to run validations for each of the above points and revert the whole transaction in case any step fails.
I am stuck on the following questions
- What would be the right pattern to achieve this during autosave?
- When and how to trigger AASM events during autosave?
- How to bubble up errors if there are any validations/guard failures?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论