pluginaweek / state_machine

Adds support for creating state machines for attributes on any Ruby class
http://www.pluginaweek.org
MIT License
3.74k stars 507 forks source link

Implementing multiple state machines (in ActiveRecord) via a has_many relationship? #294

Closed dannguyen closed 10 years ago

dannguyen commented 10 years ago

I've browsed the documentation and some of the source and didn't see this directly addressed...but here's a hypothetical use case:

I have People and Organizations, and each person has a certain state with each organization. Let's pretend it's something like a vetting process, and you want to formalize the state of how each organization has vetted each person.

To simplify things, the vetting state machine for each organization can be the same. You might have the states be something like:

So given that a person may be vetted by a variable number of organizations, it is unwieldy to store the state as a column in the people table. It seems that it requires an intermediary model, such as VettingProcess, so that a person can have many vetting_processes with organizations and vice versa.

The more that I think about it, the more it seems that the state machine should be placed on VettingProcess, right? So I suppose what I'm asking for is if there's a convention for this, but the more I think about it, that seems needlessly outside the scope of this gem, but doesn't hurt to ask :)

the8472 commented 10 years ago

The more that I think about it, the more it seems that the state machine should be placed on VettingProcess, right?

Correct. Use a has_many :through instead of a hbtm and you can easily put the state machine on the join model. No need for any special logic.