I am trying to figure out the has / belongs_to etc relationship for
rails but everyone always uses very abstract examples. Could someone
fill in the blanks on the following example I came up with over lunch?
In my actual program I never bothered filling out these relationships
but thought it might be a good idea to fix that. Do you always fill
these out and what sort of benifits do you get from it?
Say we have a school with students, teachers, classes and books.
A teacher can teach 1 to 3 classes
A student takes 6 of 10 offered classes
Each class has exactly 1 unique book
[A student:
Takes some but not all classes,
Has different teachers, but may have the same teacher multiple times
Has one book per class or 6 books]
The students could print out their booklist, schedule. The teacher
could print out a list of their classes and the students in them. etc.
A rough idea for the tables might look like so
Teachers
id, name…
Student
id, name…
Class
id, description…
schedule
id, teacher_id, student_id, class_id
Book
id, class_id
How would the models look? Here is what I came up with
Teacher
belongs_to :schedule
Student
belongs_to :schedule
Class
belongs_to :schedule
has_one :book
Schedule
has_many :teacher
has_many :student
has_many :class
Book
belongs_to :class