简体   繁体   English

Rails和state_machine宝石。 在命名空间状态机中使用回调来触发其他计算机中的事件

[英]Rails and the state_machine gem. Using callbacks in namespaced state machines to trigger events in in other machines

Like the title says, I am using the state machine gem to create multiple, namespaced state machines on one model. 就像标题所说的那样,我正在使用状态机gem在一个模型上创建多个命名空间状态机。 When one of my state machines transitions to a specific state, I am trying to use a callback to trigger an event in a separate state machine on that same model but am getting an error. 当我的状态机之一转换为特定状态时,我试图使用回调在同一模型上的单独状态机中触发事件,但出现错误。

https://github.com/pluginaweek/state_machine https://github.com/pluginaweek/state_machine

This is what I am calling: 这就是我所说的:

project.status.complete_first

And this is the error I am getting: 这是我得到的错误:

NoMethodError: undefined method `start_the_second_state_machine' for #<StateMachines::Machine:0x007f9467974b60>

This is a simplified version of my code: 这是我的代码的简化版本:

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

When I remove the line self.transition_to_creative_brief , there are no errors and my first_machine object transitions, however I need the event to be called on my second_machine as well. 当我删除self.transition_to_creative_brief ,没有错误,并且first_machine对象进行了转换,但是我也需要在second_machine上调用该事件。 So, I know the issue is with self and that is is not my status object, but I am not sure how to access that? 因此,我知道问题出在self ,那不是我的身份对象,但是我不确定如何访问它?

Try the following: 请尝试以下操作:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM