I need to add status for an object, and need a hint about the Rails way to do this. Somewhere I’ve seen status was added into the model, but already lost where it was.
By status, I mean something that tracks about the item state. Like {0: :ORDERED, 1: :CHANGED, 2: :SHIPPED, 3: :ARCHIVED} for order in store. Looks like it needs id that stored in DB, constant or symbol that I could use in code instead of integer id, and one or two human readable messages for UI
There’s a couple simple ways to do this. If the names of the statuses are short, I’d do basically what Samy suggested and store them directly in the model. So, in your migration, you’d do
Then, in your model, you can use the
statusmethod to retrieve the status. You’ll want to make sure you only store valid statuses, so you the:inclusionvalidator something like this:If the statuses are longer, you can do something very much like the above with a short name for each status, then add an additional method to give you the full status message