[英]Rails and the state_machine gem. Using callbacks in namespaced state machines to trigger events in in other machines
就像标题所说的那样,我正在使用状态机gem在一个模型上创建多个命名空间状态机。 当我的状态机之一转换为特定状态时,我试图使用回调在同一模型上的单独状态机中触发事件,但出现错误。
https://github.com/pluginaweek/state_machine
这就是我所说的:
project.status.complete_first
这是我得到的错误:
NoMethodError: undefined method `start_the_second_state_machine' for #<StateMachines::Machine:0x007f9467974b60>
这是我的代码的简化版本:
class Status < ActiveRecord::Base
belongs_to :project
######### First Machine #########
state_machine :first_machine, initial: :first_pending, :namespace => 'first' do
after_transition any => :finished do |transition|
self.start_the_second_state_machine
end
event :complete do
transition first_pending: :finished
end
end
######### Second Machine #########
state_machine :second_machine, initial: :unstarted, :namespace => 'second' do
event :start_the_second_state_machine do
transition unstarted: :started
end
end
end
当我删除self.transition_to_creative_brief
,没有错误,并且first_machine对象进行了转换,但是我也需要在second_machine上调用该事件。 因此,我知道问题出在self
,那不是我的身份对象,但是我不确定如何访问它?
请尝试以下操作:
class Status < ActiveRecord::Base
belongs_to :project
######### First Machine #########
state_machine :first_machine, initial: :first_pending, :namespace => 'first' do
after_transition any => :finished do |status, transition|
status.start_the_second_state_machine
end
event :complete do
transition first_pending: :finished
end
end
######### Second Machine #########
state_machine :second_machine, initial: :unstarted, :namespace => 'second' do
event :start_the_second_state_machine do
transition unstarted: :started
end
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.