views:

74

answers:

3

I'm creating this little show scheduler thing.

I have the table shows, with title:string and description:text. I want to add days and times to the show (each show can have multiple days, and each day has their OWN times). How might I go about doing this?

I was thinking of making a times table. With the columns (show_id, day, and time). Then when listing the show, you search for times that meet that show_id and sort by day.

Any ideas, suggestions, etc. welcomed.

A: 

I think your idea is very reasonable (especially for someone who has no experience with programming). If you're interested in "Rails way" of implementing this, I would recommend checking has_many relationship in the Rails guide. (And I do recommend this guide in general, as it helped me a lot with Rails)

Nikita Rybak
+2  A: 

You'd create the Show model with the following command:

script/generate model Show title:string description:text

Then you'd create the ShowTime model:

script/generate model ShowTime date:datetime time:datetime

Then, in your show.rb model file, you'd define a has_many relationship like this:

 has_many :showtimes

And in your ShowTime model file, you'd define a belongs_to relationship like this:

 belongs_to :show
Jacob Relkin
I think your ShowDate and ShowTime models should include show_id:integer in order to make the relationship work? And shouldn't it be `belongs_to :show` (lower-case "s")?
Mike Woodhouse
Why would you separate the Date from the Time? Wouldn't it make more sense to just combine these into a single DateTime field? Then each show time can have a date and a unique time for that date, without needing to manage them separately.
Beerlington
Sorry if I might have confused you guys in my post, or maybe I'm misunderstanding.But, wouldn't this require that I add each time and day specifically? (Like I add "9/13/10 5:30", "9/20/10 5:30", and "9/27/10 5:30"?)I want to be able to just add the day of the week, and the time for that day.
Rickmasta
+1  A: 

Seems to me the simplest solution to this would be to use a single DateTime Object, since you can get the day and the time from a Time object.


script/generate model ShowTime time:datetime
script/generate model Show title:string description:text showtime:references

Then put in the belongs_to/ has many_associations that Jacob referred to. I think having a separate model for the showtime would be better and allow for more flexibility. You should also look into the strftime method on a Time object, to format the time to your liking.

Mysrt
Out of curiosity, why bother with ShowTime? Why not just add time:datetime to the Show model? It seems more like a property of show then its own thing
Matt Briggs
@Matt, the OP wants to be able to have many times associated with **one** `Show`, that's why.
Jacob Relkin