简体   繁体   中英

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. 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

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. 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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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