Reputation: 1553
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,
}
Upvotes: 15
Views: 12658
Reputation: 12273
Here's a solution that removes the magic number.
Order.states.key(Order.states[:cancelled]) => 'cancelled'
Upvotes: 5