# One to Many Relational database

**URL:** <https://www.ruby-forum.com/t/one-to-many-relational-database/142708>\
**Category:** Rails\
**Created:** [July 10, 2008, 4:57pm UTC](https://www.ruby-forum.com/t/one-to-many-relational-database/142708 "2008-07-10T16:57:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![advait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e19adc/32.png) [@advait](https://www.ruby-forum.com/u/advait)\
**Post date:** [July 10, 2008, 4:57pm UTC](https://www.ruby-forum.com/t/one-to-many-relational-database/142708/1 "2008-07-10T16:57:59Z")

</div>

Hi All,

```
  I have established the relationship and everything is working

```

well, but I need to display as descried below.

I have two tables Student and Teacher.

In my application one teacher has several students under him, so how do  
I display the students under only that particular teacher at a given  
time.

Thank You

---

<div class="post-metadata">

**Author:** ![advait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e19adc/32.png) [@advait](https://www.ruby-forum.com/u/advait)\
**Post date:** [July 10, 2008, 5:59pm UTC](https://www.ruby-forum.com/t/one-to-many-relational-database/142708/2 "2008-07-10T17:59:41Z")

</div>

teacher model:  
has\_many :students

student model:  
belongs\_to :teacher

in controller:  
@teacher = Teacher.find(…your code…)

then all his students can be accessed:  
@teacher.students

for example  
@teacher.students.each do |student|  
…do something with student  
end

---

<div class="post-metadata">

**Author:** ![advait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e19adc/32.png) [@advait](https://www.ruby-forum.com/u/advait)\
**Post date:** [July 12, 2008, 8:16am UTC](https://www.ruby-forum.com/t/one-to-many-relational-database/142708/3 "2008-07-12T08:16:41Z")

</div>

Thorsten M. wrote:

> teacher model:  
> has\_many :students
> 
> student model:  
> belongs\_to :teacher
> 
> in controller:  
> @teacher = Teacher.find(…your code…)
> 
> then all his students can be accessed:  
> @teacher.students
> 
> for example  
> @teacher.students.each do |student|  
> …do something with student  
> end

How to student as a nested resource to teacher.  
Such that when I perform a show action an albums, I should be able to  
view only the photos associated with it and not all the photos of the  
table
