Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get key name from a enum value in Rails?

I have a enum in my Model that corresponds to column in the database.

The enum looks like:

enum efficency: { High: 0, Medium: 1, Low: 2 }

How can I get the key from the value

For example, i have the value 0, i need to fetch the value High.

Any help would be appreciated.

like image 504
prajeesh Avatar asked Jul 28 '17 12:07

prajeesh


1 Answers

Model.efficiencies.key(0) # => 'High'

Replace Model with the actual name of your model.

like image 200
Tom Lord Avatar answered Sep 17 '22 19:09

Tom Lord