How to set up two models having a has_many association with each other
Posted
by
daz13
on Stack Overflow
See other posts from Stack Overflow
or by daz13
Published on 2011-01-05T05:42:42Z
Indexed on
2011/01/05
5:53 UTC
Read the original article
Hit count: 147
I'm looking for a suggestion on how to set up two models, Teacher and Subject. A Teacher can have many Subjects, and a Subject can have many Teachers. Another thing to consider in the relationship between the two models is that a Teacher can create a Subject and add other Teachers to the Subject.
I think I'm solid on the basics of the set up for each model:
for teacher.rb:
has_many :subjects
for subject.rb:
has_many :teachers
and the teachers table should have a subject_id column and the subject table should have a teacher_id column.
What I'm not sure about is how to set up the views (and corresponding controller methods) to allow the addition of a Teacher to a Subject.
Any suggestions (or links to examples) are greatly appreciated. I haven't been able to find anything on this exact case.
© Stack Overflow or respective owner