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!
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.
[email protected]
2009/6/2 Jack C. [email protected]:
Create a days_demonstrations table and create a HABTM relationship
possibly work and all that.
–
Jack C.
[email protected]
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
2009/6/2 sir ziggles [email protected]:
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
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?
2009/6/2 sir ziggles [email protected]:
| 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
Colin L. wrote:
2009/6/2 Jack C. [email protected]:
Create a days_demonstrations table and create a HABTM relationship
possibly work and all that.
–
Jack C.
[email protected]
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!
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?
god, i love railscasts checking it out now.
thank you again colin!
2009/6/2 Jeff S. [email protected]:
…
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