I’m quite new to Rails and I’ve got a project to do that requires some
many-to-many relations right at the beginning. I’ll describe what I
want to do, maybe you could tell me how to realize that.
I have the tables »people« and »courses«. One course can have multiple
people (I’m talking of teachers here) and one person can be in
multiple courses. As I started creating my application in the RESTful
way and I learned that the »has_and_belongs_to_many« association
doesn’t work very well with the REST-approach, I created a third table
and model called »teachers« that is the join model between those two,
so the models look like this:
class Course < ActiveRecord::Base
has_many :people, :through => :teachers
class Person < ActiveRecord::Base
has_many :courses, :through => :teachers
class Teacher < ActiveRecord::Base
Now following the RESTful way I should add teachers only through my
»TeachersController«, right? Now what I’d like to do is the following.
When you are creating a new course, then on the same page with the
course form (so /courses/new ) there should be the possibility to add
people as teachers to the course. Then if you edit this course
afterwords it should show you the teachers of this course and make it
possible to get them out of this course and add new ones to the
course. But how can I have that on the same page as the courses? How
do I create a new teacher on the same page where the new or edit form
of a specific course is?
I have really no clue. I mean I don’t insist on having the »Add
Teacher« thing on the new course page, but what I don’t want, is to
put it on an own page only for adding a teacher.
Would be very happy about some answers.