get list of state_machine states

2019-01-17 13:57发布

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!

3条回答
欢心
2楼-- · 2019-01-17 14:26

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
查看更多
等我变得足够好
3楼-- · 2019-01-17 14:30

Something like:

User.state_machine.states.map &:name
查看更多
贪生不怕死
4楼-- · 2019-01-17 14:35

If you name your state field differently , such as publish_status

Article.state_machines[:publish_status].states.map &:name
查看更多
登录 后发表回答