# Actuve Record

**URL:** <https://www.ruby-forum.com/t/actuve-record/197032>\
**Category:** Ruby\
**Created:** [March 25, 2010, 12:19pm UTC](https://www.ruby-forum.com/t/actuve-record/197032 "2010-03-25T12:19:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mailtorajuit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed8c4c/32.png) [@mailtorajuit](https://www.ruby-forum.com/u/mailtorajuit)\
**Post date:** [March 25, 2010, 12:19pm UTC](https://www.ruby-forum.com/t/actuve-record/197032/1 "2010-03-25T12:19:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mailtorajuit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed8c4c/32.png) [@mailtorajuit](https://www.ruby-forum.com/u/mailtorajuit)\
**Post date:** [March 25, 2010, 6:43pm UTC](https://www.ruby-forum.com/t/actuve-record/197032/2 "2010-03-25T18:43:55Z")

</div>

On Thu, Mar 25, 2010 at 5:19 AM, Rajkumar S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> ## how to do this
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Hi, Rajkumar, this is the Ruby mailing list, you probably want the Ruby  
on  
Rails list at [http://groups.google.com/group/rubyonrails-talk](http://groups.google.com/group/rubyonrails-talk)

The associations guide at  
[Active Record Associations — Ruby on Rails Guides](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.
