# Day of week but stored as number in the DB

**URL:** <https://www.ruby-forum.com/t/day-of-week-but-stored-as-number-in-the-db/145947>\
**Category:** Rails\
**Created:** [August 15, 2008, 11:58am UTC](https://www.ruby-forum.com/t/day-of-week-but-stored-as-number-in-the-db/145947 "2008-08-15T11:58:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [August 15, 2008, 11:58am UTC](https://www.ruby-forum.com/t/day-of-week-but-stored-as-number-in-the-db/145947/1 "2008-08-15T11:58:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [August 15, 2008, 12:02pm UTC](https://www.ruby-forum.com/t/day-of-week-but-stored-as-number-in-the-db/145947/2 "2008-08-15T12:02:50Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [August 15, 2008, 12:22pm UTC](https://www.ruby-forum.com/t/day-of-week-but-stored-as-number-in-the-db/145947/3 "2008-08-15T12:22:03Z")

</div>

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}

---

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [August 15, 2008, 7:06pm UTC](https://www.ruby-forum.com/t/day-of-week-but-stored-as-number-in-the-db/145947/4 "2008-08-15T19:06:53Z")

</div>

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

Eric

On Aug 15, 4:22 am, Tim C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
