Thursday 15 March 2012

Modelling states and sub-states using enums in rails -



Modelling states and sub-states using enums in rails -

i have object has state. can be:

'scheduled', 'on-shift' or 'past'

in addition, 'on-shift' can of state:

'available', 'busy' or 'inactive'

what best way model these states in rails app?

is to:

a. create 2 state attributes:

1. object_state - can take value: 'scheduled', 'on-shift' or 'past' 2. on_shift_state - can take value: 'available', 'busy' or 'inactive'

b. create single state attribute, can take value:

'scheduled', 'past', 'available', 'busy' or 'inactive'.

c. other way.

i think alternative b ensure logical consistency in database, increment complexity of queries, e.g. if want search units 'on-shift', need search 3 different states. may harder extend, e.g. if want add together more states @ later date.

if go alternative b, there way simplify queries?

any specific structures/methods suggest using?

note: thinking of using enums model state(s).

i recommend using state machine , storing states in single attribute.

with state machine need add together state field model , can define states using gem's dsl. state machine handle logic of transitioning between states , provide nice getters , setters each state.

ruby-on-rails enums state-machines states

No comments:

Post a Comment