prajeesh
prajeesh

Reputation: 2382

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.

Upvotes: 19

Views: 22858

Answers (1)

Tom Lord
Tom Lord

Reputation: 28305

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

Replace Model with the actual name of your model.

Upvotes: 35

Related Questions