Actuve Record

HI all

in my application , i have 2 tables subjects and teachers. the relation
is one to many.

i have to display the subjects first and when i click on the subjects i
have to display the teachers.

and i have to add the new teacher to the teachers table by selecting the

how to do this

On Thu, Mar 25, 2010 at 5:19 AM, Rajkumar S.
[email protected]wrote:

how to do this

Posted via

Hi, Rajkumar, this is the Ruby mailing list, you probably want the Ruby
Rails list at

The associations guide at talks about what
are asking. The short answer is that subjects will have a foreign key of
teachers, and in your model you say “belongs_to :teacher” and in your
teacher model you say “has_many :subjects”

Then from a subject, you can get it’s teacher with subject.teacher and
a teacher you can get it’s subjects with teacher.subjects. To link them
just use the setters. subject.teacher = current_teacher.

You should consider, though, whether this design is really appropriate.
my university, a subject may be taught by multiple teachers. For
there are usually between 5 and 10 calculus courses, each one taught by
different teacher. You may want to add a courses table, where each
belongs to one subject, and one teacher, and then you can have a many to
many relationship between subjects and teachers, through the courses.
should all be talked about on the guide I linked you to, but really the
Rails ML is where you should ask future questions about ActiveRecord.