This is probably a simple question, but I’m trying to lookup the constant name in Ruby from the value. For example:
class Xyz < ActiveRecord::Base
ACTIVE = 1
PENDING = 2
CANCELED = 3
SENT = 4
SUSPENDED = 5
end
I have a status of 1 in my db. I want to retrieve ACTIVE based on this so that I can display it in a view.
What’s a good way to do that?
However, I wouldn’t do this: using programmatic names as values for a view seems like a bad idea. You’re likely to run into a situation where you want to change the display name (maybe “suspended” should be shown as “on hold”) and then you have to refactor your code.
I’d put a mapping in your view or controller, using the constants from the model: