Rails 和 state_machine gem。在命名空间状态机中使用回调来触发其他机器中的事件
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 object 转换,但是我需要在我的 second_machine 上调用该事件作为出色地。所以,我知道问题出在 self
而不是我的状态 object,但我不确定如何访问它?
尝试以下操作:
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
如标题所说,我正在使用状态机 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 object 转换,但是我需要在我的 second_machine 上调用该事件作为出色地。所以,我知道问题出在 self
而不是我的状态 object,但我不确定如何访问它?
尝试以下操作:
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