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.
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
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.
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)
Related
It is my first question, but I have yet to find an answer, so I hope it doesn't violate any rule.
I have a problem with a seemingly simple rails issue. I have taken the time to read about relationship models in rails (has_many :through) and came upon this example:
Exemplary model relations
In my model, I have Anthology (phyisicians), Poem(patients), and an anthology_poem relationship model (appointments). In may relationship table, I have a column, order, that indicates the position of a specific poem in a specific anthology.
The question is - How do I address said "order" column? How do I update it/read it? I imagine something like:
book.poems.first.order
which obviously doesn't work.
I'd like to be able to do it without too much hacking, because I fell in love with how simply rails handled the rest of the stuff.
Thanks in advance!
If you want to access your relationship model attribute you should call it on that model:
Appointment.where(physician: physician).pluck(:order)
Currently I am building a homepage for my local soccer club and I want to save training times for each team. The training times will look something like this:
Tuesday 18:00-19:30
Thursday 18:30-20:00
What is the best way to store these values inside the database in rails?
I recommend creating a Practice model, with each practice having a :start and :end attribute, each typed as a :datetime. If you generate a migration like so:
rails generate model Practice start_time:datetime finish_time:datetime
That will build a migration for your database, adding the columns you need. Be sure to run rake db:migrate to update your development database.
You would also have to link this new model to your Team model. The relationship between teams and practices seems to be one-to-many, so you'd add this to your Team class
class Team < ActiveRecord::Base
has_many :practices
end
and add the corresponding relationship to your Practice model
class Practice < ActiveRecord::Base
belongs_to :team
end
(Note the different use of singular and plural class names in these methods.)
You might then build a method within your Practice class to render a formatted date and time range from these two attributes.
def practice_time
formatted_str = start_time.strftime("%A %H:%M") + " " + finish_time.strftime("%A %H:%M")
end
You can see more options for the strftime method here
Once you have stored your date information in a logical way, feel free to develop model and/or helper methods to return your data in a more useful form. You might also consider some add-ons like ActiveAdmin to make entering and searching for dates easier.
Here's my solution(may or may not sounds good to you). Create two time columns each represents From and To timings of a day.for example
for Monday it goes like this in your database
|monday_from(time property)|monday_to(time property)|and so on
+--------------------------+------------------------+
Hope this helps you.
Edit: I considered that the training events are repeated in time regularly, so you don't have to stick to a particular date.
You can simply save it to the database as a string.
You can also make a new model (or table) with attributes, start time and end time. Then you can save a Time object to the database.
When loading these Time objects, you can create a helper for view to format the time. It can look the following:
def format_time(time)
time.strftime("%A %H:%M")
end
I added a relationship a little while ago to users (user has many cars).
But my boss has told me I need to make a new model for companies which the
cars will belong to the company and the leases will reside with the company so
company leases to many uses.
I can't find the specific migration that added the user_id to cars,
so is there a way to say modify?
rails g migration user_id_to_company_id_on_cars
Sorry, the question is hard to understand. Why should you modify rails association from command line, I think it is just belongs_to or has_many in the model?
If you want to do a migration on a model, you can do for example:
rails g migration add_user_id_to_cars user_id:integer
I have a question regarding a simple app I am developing in Rails 3.1, but I am not sure of how to define the associations and am looking for advice on the best way to achieve the following.
I have tapes, each tape contains a barcode.
I have boxes, each box has a name
What i need to track is the act of associating the two , each day up to 24 tapes will be placed into a Box, so i need a way of creating a new association for existing boxes and existing tapes on a form, and ideally track some extra information.
The tapes are on a 5 week cycle, so after 5 weeks a tape will ( possibly ) be associated with a new box.
I am not sure how to model the associations, any help or input would be appreciated
Do you need to track which box it used to belong to? If not, this is a simple belongs_to/has_many association.
class Tape
belongs_to :box
end
class Box
has_many :tapes
end
If you need to track past boxes, then you'll need a join-model:
class WhateverYouWant
belongs_to :box
belongs_to :tape
end
and change the box/tape associations to use ":through"
Currently, I'm building a scheduling system for my workplace. It's a highly-trafficked university tour guide office, and given a schedule of the day's events and the shifts of all available tour guides, the system will assign tours to available tour guides.
But, when asking a user to build the event schedule (or list of tours) for the day, I'd prefer to have a schedule auto-generated that they can then manipulate. There are only two different types of schedules that alternate depending on the day of the week, and this head start will save a lot of time.
My question: Where do I put this 'seed' data? Should I create a YAML and then have a method read it to populate a given day with appropriate defaults?
For reference, my models are currently structured like so:
guide has_many :tours, :shifts
shifts belongs_to :guide
tour belongs_to :guide
I've considered a 'day' model but as every time is a datetime, I felt like this was too redundant to persist to the database.
Thanks! All suggestions welcome. I'm just trying to get a good grip on the proper "Rails Way" to handle this.
You should use a factory to initialize your objects. Have a look at https://github.com/thoughtbot/factory_girl