get list of state_machine states

Ruby on-RailsState Machine

Ruby on-Rails Problem Overview


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!

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Something like:

User.state_machine.states.map &:name

Solution 2 - Ruby on-Rails

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

Solution 3 - Ruby on-Rails

If you name your state field differently , such as publish_status

Article.state_machines[:publish_status].states.map &:name

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionphilView Question on Stackoverflow
Solution 1 - Ruby on-RailsjwarcholView Answer on Stackoverflow
Solution 2 - Ruby on-RailsfakingfantasticView Answer on Stackoverflow
Solution 3 - Ruby on-RailswizztjhView Answer on Stackoverflow