Forum: Ruby on Rails Days of the Week and Models

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
sir z. (Guest)
on 2009-06-02 10:14
Hi All,

Would love to hear your input on this:

I have a model called Demonstration.

A Demonstration can take place on any, some or all of the days in a
given week.

i.e. Monday, Tuesday and Friday

How should I setup my model(s) to support this relationship?  Is there a
preferred method??




Two ideas i've had:
 - Create a model named Days with a row for each day of the week.
Create a days_demonstrations table and create a HABTM relationship

 - Use the built in Date::DAYNAMES that comes with Ruby.  Create a field
in the Demonstration table called 'days' and store the supported days as
a string: 0,3,5.  Then break it up when it's time to use them as check
boxes in the view.


thanks!
Jack C. (Guest)
on 2009-06-02 17:06
(Received via mailing list)
sir ziggles wrote:
>
>  - Use the built in Date::DAYNAMES that comes with Ruby.  Create a field
> in the Demonstration table called 'days' and store the supported days as
> a string: 0,3,5.  Then break it up when it's time to use them as check
> boxes in the view.
>
>
> thanks!
>
I just use a boolean column for each day. Simplest thing that can
possibly work and all that.

--
Jack C.
removed_email_address@domain.invalid
Colin L. (Guest)
on 2009-06-02 17:51
(Received via mailing list)
2009/6/2 Jack C. <removed_email_address@domain.invalid>:
>>
>> Create a days_demonstrations table and create a HABTM relationship
> possibly work and all that.
>
> --
> Jack C.
> removed_email_address@domain.invalid
>

I think I would do the same, then you can easily have a set of
checkboxes for the user to specify the days if that is appropriate.

Colin
sir z. (Guest)
on 2009-06-02 18:00
Colin L. wrote:
> 2009/6/2 Jack C. <removed_email_address@domain.invalid>:
>>>
>>> Create a days_demonstrations table and create a HABTM relationship
>> possibly work and all that.
>>
>> --
>> Jack C.
>> removed_email_address@domain.invalid
>>
>
> I think I would do the same, then you can easily have a set of
> checkboxes for the user to specify the days if that is appropriate.
>
> Colin

Colin, i'm sorry but your response (at least on ruby-forum.com) is
quoting both of the potential methods i had mentioned...

which one were you saying is the preferred way?


and big thanks to jack and colin for taking time to respond!
Colin L. (Guest)
on 2009-06-02 18:26
(Received via mailing list)
2009/6/2 sir ziggles <removed_email_address@domain.invalid>:
>>>
>>
>> I think I would do the same, then you can easily have a set of
>> checkboxes for the user to specify the days if that is appropriate.
>>
>> Colin
>
> Colin, i'm sorry but your response (at least on ruby-forum.com) is
> quoting both of the potential methods i had mentioned...
>
> which one were you saying is the preferred way?

I think the crucial text has been snipped.  My comment is immediately
after Jack C.'s. My intention was to indicate that I would do
it the same way as him. I should have been a bit more explicit.

Colin
sir z. (Guest)
on 2009-06-02 19:47
ah got ya.

thinking out loud here...

The Demonstration table might look like:
 ---------------
| Demonstration |
 ---------------
| id:integer    |
| title:string  |
| descript:text |
| mon:boolean   |
| tues:boolean  |
| wed:boolean   |
| etc etc       |
 ---------------


The only drawback I can see is that if I decided to add something like a
Presentation model that also occurs on various days in the week I would
have to add those same day columns to the Presentation model and it
doesn't seem very DRY?
Colin L. (Guest)
on 2009-06-02 20:08
(Received via mailing list)
2009/6/2 sir ziggles <removed_email_address@domain.invalid>:
> | title:string  |
> have to add those same day columns to the Presentation model and it
> doesn't seem very DRY?
>

I think if you wanted day selection in more than one model you would
do it using a polymorphic model relationship (see
http://railscasts.com/episodes/154-polymorphic-association for
example).  Still using a set of booleans in the DaySelectable model or
whatever you call it.  Alternatively, dependent on how similar
Demonstration and Presentation were they could be combined into one
model or use Single Table Inheritance.

Colin
sir z. (Guest)
on 2009-06-02 20:13
god, i love railscasts :)  checking it out now.

thank you again colin!
Jeff S. (Guest)
on 2009-06-02 20:13
(Received via mailing list)
Jack C. wrote:
> sir ziggles wrote:

>> I have a model called Demonstration.
>>
>> A Demonstration can take place on any, some or all of the days in a
>> given week.
>>
>> i.e. Monday, Tuesday and Friday
>>
>> How should I setup my model(s) to support this relationship?

> I just use a boolean column for each day.

Is there typically any space penalty (or advantage) for separate,
boolean columns, relative to an integer bit-field?
Colin L. (Guest)
on 2009-06-02 20:22
(Received via mailing list)
2009/6/2 Jeff S. <removed_email_address@domain.invalid>:
>
> ....
>
> Is there typically any space penalty (or advantage) for separate,
> boolean columns, relative to an integer bit-field?
>

Yes I am sure there is a space penalty.  Disk space and processor
power are cheap, skilled manpower is expensive.  I would do it the way
that is easiest to implement and maintain and worry about efficiency
only if it becomes a problem (which it usually will not), and I am old
enough to remember writing in machine code and worrying about every
byte and processor cycle.

Colin
This topic is locked and can not be replied to.