Actuve Record

R

Rajkumar Surabhi

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
subject.

how to do this
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

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
subject.

how to do this
Hi, Rajkumar, this is the Ruby mailing list, you probably want the Ruby on
Rails list at http://groups.google.com/group/rubyonrails-talk

The associations guide at
http://guides.rubyonrails.org/association_basics.html talks about what you
are asking. The short answer is that subjects will have a foreign key of the
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 from
a teacher you can get it's subjects with teacher.subjects. To link them up,
just use the setters. subject.teacher = current_teacher.

You should consider, though, whether this design is really appropriate. At
my university, a subject may be taught by multiple teachers. For example,
there are usually between 5 and 10 calculus courses, each one taught by a
different teacher. You may want to add a courses table, where each course
belongs to one subject, and one teacher, and then you can have a many to
many relationship between subjects and teachers, through the courses. That
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top