就像标题所说,我使用状态机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
,这不是我的状态对象,但我不确定如何访问它?
答案 0 :(得分:1)
尝试以下方法:
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