Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to get enum string name from integer value

I need to get string name of enum state by integer value, and i do it next way

Order.states.find{|x| x[1] == data['stateId']}

Does anybody know better way to do it?

enum state: {
         created: 0,
         cancelled: 100,
         complete: 10,
       }
like image 224
eugene_trebin Avatar asked Mar 16 '17 16:03

eugene_trebin


2 Answers

Order.states.key(100) => 'cancelled'
like image 168
eugene_trebin Avatar answered Sep 22 '22 01:09

eugene_trebin


Here's a solution that removes the magic number.

Order.states.key(Order.states[:cancelled]) => 'cancelled'
like image 34
Dty Avatar answered Sep 21 '22 01:09

Dty