Day of week but stored as number in the DB

Hi, I have a 'day' column in one of my models. This is stored in the DB as an integer value 1..7. I want this field to appear as a string whenever it appears in a view e.g. if the DB has stored 1 I want the view to display 'Monday' 2 => Tuesday etc. etc.

How is this done in Rails? The value in the DB needs to be originally set by the user using a drop down list with the options Monday..Sunday.

Basically, the user always needs to interact with a day string, but I always want to store the corresponding integer in the DB. Thanks in advance

You can create a DAYS_OF_THE_WEEK constant in config/environment.rb and reference this in your application. To get the relevant day of the week just do DAYS_OF_THE_WEEK[variable].

Example:

DAYS_OF_THE_WEEK = %w[sunday monday tuesday wednesday thursday friday saturday]

>> DAYS_OF_THE_WEEK[1] => "monday"

Thanks for the help. I guess I can then use the following as the choices in the selection list (in order to get the correct id passed in the params):

DAYS_OF_THE_WEEK_CHOICES = DAYS_OF_THE_WEEK.map_with_index{|day,i| day.to_a << i}

FYI, there's already a constant defined in Ruby's Date class: Date::DAYNAMES

Eric