In Rails 4.2 I can write:
class Task < ActiveRecord::Base
enum priority: { normal: 50, low: 0, high: 100 }
end
If I have a SQL query which (for obscure reasons) returns the priority as an integer, is there a simple way to convert that integer into its corresponding enumerated value, eg. :high
?
(Obviously I can look up the key from the value in Task.priorities
, or define my own reverse index, but presumably this is already implemented somewhere in Rails...)
Apparently in ruby 1.9+ I can write:
Task.priorities.key(100)
=> "high"
No idea if this is efficient, but since the hash table is small...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With