1

I am using Rail 4.2.3 and aasm gem version 4.1 Is it possible to add two state machines in one model for enum fields?

I have enum state: [:active, :suspended, :deleted] and enum activity: [:working, :stopped]

and I wont 2 state machines like this:

aasm(:connection_state, column: :state, enum: true do
    state :active, initial: true
    state :suspended
    state :deleted

    event :activate do
      transitions from: :suspended, to: :active
      transitions from: :deleted, to: :active
    end

    event :suspend do
      transitions from: :active, to: :suspended
    end

    event :mark_as_deleted do
      transitions from: [:active, :suspended], to: :deleted
    end
  end

and other state machine:

aasm(:activity_state, column: :activity, enum: true do
    state :working, initial: true
    state :stopped

    event :start_working do
      transitions from: :stopped, to: :working
    end

    event :stop_working do
      transitions from: :working, to: :stopped
    end
  end

but specs are failing with error:

expect(subject).to transition_from(:active).to(:suspended).on_event(:suspend)

AASM::UnknownStateMachineError: There is no state machine with the name 'default' defined in ModelName

What I am missing?

user1136228
  • 967
  • 9
  • 22

1 Answers1

0

There was problem in my spec, just not readed through docs carefully:

expect(subject).to transition_from(:active).to(:suspended).on_event(:suspend).on(:connection_state)

fixes the issue

user1136228
  • 967
  • 9
  • 22