[英]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.