I want to store Monday through Sunday and currently I am using a smallint but I am wondering what would be the best way to represent this in my model? Basically, I want to be able to display the words “Monday->Sunday” but realize that this is not the smartest way to store it in the db. Should I just use an associative array?
Share
There are certainly a number of ways to do this. If you are always going to be storing consecutive days, then maybe two separate database columns representing the start and end dates(index of 1-7) would be in order (with a method to translate from day index to textual name).
This may work for your situation, however, I find that more often you will need the flexibility of inserting mulitiple non-consecutive days. One method that I have used recently (in an effort to minimize SQL queries), is to simply add 7 boolean database fields to your table representing Sun-Mon. This would allow you to use methods such as @event.monday? A helper method could lump consecutive days together for display purposes.