Multiple state machines in one model?
Asked Answered
E

3

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?

Eucharis answered 24/2, 2010 at 0:57 Comment(0)
A
-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...

Ashton answered 7/9, 2010 at 7:52 Comment(2)
there are multiple models for all these things, and it's the overall process that needs the sate modelingEucharis
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.Mercurate
L
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.

Lecturer answered 12/9, 2016 at 11:18 Comment(0)
A
4

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

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

Analemma answered 24/2, 2010 at 2:28 Comment(0)
A
-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...

Ashton answered 7/9, 2010 at 7:52 Comment(2)
there are multiple models for all these things, and it's the overall process that needs the sate modelingEucharis
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.Mercurate

© 2022 - 2024 — McMap. All rights reserved.