On Thu, Aug 23, 2007 at 07:38:27PM -0000, Zeekar wrote:
I have a class which stores as one of its attributes a day of the week
as an integer (0-6, 0=Sunday, standard UNIXy values). I want,
obviously, the views to show the weekday name and the forms to provide
a drop-down with the weekday names. Essentially it’s a vrtual
attribute whose value is the localized weekday name, which maps to the
integer value that’s actually stored.
There are two pieces to this. One is how you deal with the data readably
your code, the other is rendering a localized version.
There are a lot of approaches, but I was wondering what the idiomatic
Rails-y way of doing it is. Should I create a Weekday class? It
wouldn’t be backed by the db - that’s not localizable or DRY…
In your code, the idiomatic way is to use an array:
DAYS = %w(sunday monday tuesday wednesday thursday friday saturday)
You should override your accessors that otherwise return a number 0-6 to
instead return that DAYS[value] (or, more likely, value && DAYS[value]).
Now you can deal with days by string. (Probably a better choice is to
that an array of symbols and deal with them by symbol.) Once you get to
rendering phase, just have your local substitutions based on the
Any help appreciated.