51

We are using state_machine ( https://github.com/pluginaweek/state_machine ) in a project and would like to offer a form with a select that lets us choose a state. (this is for searching, not for setting). I can't seem to find a way to list out all the possible states? Surely there should be an automatic way of doing this, not having to hard-code a list of text somewhere. Thanks!

phil
  • 4,668
  • 4
  • 33
  • 51

3 Answers3

78

Something like:

User.state_machine.states.map &:name
jwarchol
  • 1,906
  • 15
  • 12
72

Just to add to this (because I'm constantly searching for this answer, and I always come back to this page), if you are trying to list the states for a giving state machine, try this:

Discussion.state_machines[:name_of_state].states.map &:name
fakingfantastic
  • 1,551
  • 1
  • 12
  • 15
  • 4
    Thank you, this is much better. The accepted answer only handles the defaults I guess, and I needed the states for my custom field "status". Thanks again! – Jaime Bellmyer Mar 30 '11 at 19:55
  • Make sure this line comes after the state machine declarations if using it in your class. – Paul Sturgess Aug 25 '11 at 15:55
5

If you name your state field differently , such as publish_status

Article.state_machines[:publish_status].states.map &:name
wizztjh
  • 6,979
  • 6
  • 58
  • 92