8

I have a model that represents a registration process, which needs to track the progression of several processes (background checks, interviews, information collection...). Each one can be represented by a state machine, and then the overall state of the registration might depend on the state of the others.

Can aasm handle this? Any other ideas or design considerations?

DGM
  • 26,629
  • 7
  • 58
  • 79

3 Answers3

5

Since this question comes up when you google multiple state machines in one model, it would be good to have an answer, although there is already an answer. Please try doing:

class Example < ActiveRecord::Base
 include AASM

    aasm :search, :column => :search do
    state :initialised, :initial => true
    ...
    end  
    aasm :sync, :column => :sync do
    state :unsynced, :initial => true
    ...
    end 
end

This will create two state machines based on two columns, search and sync.

Darshan
  • 53
  • 1
  • 5
4

Try this plugin, which you can use to even inherit from other state machines:

http://api.pluginaweek.org/state_machine/

Josh Delsman
  • 3,022
  • 1
  • 18
  • 28
-2

If you need multiple statemachines in one model it's probably getting to big and it's time to split the model. So in your example you should add models for background checks, interviews, information collection...

Petrik de Heus
  • 970
  • 6
  • 9
  • there are multiple models for all these things, and it's the overall process that needs the sate modeling – DGM Sep 07 '10 at 14:12
  • 2
    a need for multiple statemachines does not necessarily represent a need for decomposition at all. In fact, it may result from good decomposition having already been applied. For example in the OP's usecase. It's fine if the the model on the end of a has-many has its own state-machine, but you'll still need a state-machine on the parent for the state of the collection, unless you intend to only use a lot of difficult to audit conditional transitions. – Michael Johnston Nov 22 '13 at 04:48