# Loading data from two associated tables

**URL:** <https://www.ruby-forum.com/t/loading-data-from-two-associated-tables/120379>\
**Category:** Rails\
**Created:** [November 16, 2007, 11:22am UTC](https://www.ruby-forum.com/t/loading-data-from-two-associated-tables/120379 "2007-11-16T11:22:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![katsuo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecc23a/32.png) [@katsuo](https://www.ruby-forum.com/u/katsuo)\
**Post date:** [November 16, 2007, 11:22am UTC](https://www.ruby-forum.com/t/loading-data-from-two-associated-tables/120379/1 "2007-11-16T11:22:50Z")

</div>

Hello,  
I would like to list all the chapter description related to a particuler  
title on catalog/list.rhtml. There are two associated tables ‘Title’ and  
‘Chapter’. I have written the following but does not work. Can anyone  
assist me on this issue?

#Title table  
book\_name :string  
chapter\_id :integer

#Chapter table  
description :string

class Title \< ActiveRecord::Base  
has\_many :chapters

class Chapter \< ActiveRecord::Base  
belongs\_to :title

class CatalogController \< ApplicationController  
def list  
@title = Title.find(params[id])  
end

list\_rhtml

 \<% for title in @titles %\> \<% end %\>

| \<%= link\_to title.book\_name, :action =\> 'show', :id=\> title %\> | &nbsp; | \<%= title.chapter.description %\> |

---

<div class="post-metadata">

**Author:** ![katsuo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecc23a/32.png) [@katsuo](https://www.ruby-forum.com/u/katsuo)\
**Post date:** [November 16, 2007, 11:38am UTC](https://www.ruby-forum.com/t/loading-data-from-two-associated-tables/120379/2 "2007-11-16T11:38:48Z")

</div>

if title has\_many chapters, then chapters need a title\_id, not titles a  
chapter\_id

if you correct this, then in your view this should do the trick:

 \<% @titles.each do |title| %\> \<% title.chapters.each do |chapter| %\> \<% end %\> \<% end %\>

| \<%= link\_to title.book\_name, :action =\> 'show', :id=\> title %\> | &nbsp; | \<%= chapter.description %\> |

---

<div class="post-metadata">

**Author:** ![katsuo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecc23a/32.png) [@katsuo](https://www.ruby-forum.com/u/katsuo)\
**Post date:** [November 18, 2007, 6:42am UTC](https://www.ruby-forum.com/t/loading-data-from-two-associated-tables/120379/3 "2007-11-18T06:42:20Z")

</div>

Thorsten M. wrote:

> if title has\_many chapters, then chapters need a title\_id, not titles a  
> chapter\_id
> 
> if you correct this, then in your view this should do the trick:
> 
> \<% @titles.each do |title| %\> \<% title.chapters.each do |chapter| %\> \<% end %\> \<% end %\>
> 
> | \<%= link\_to title.book\_name, :action =\> 'show', :id=\> title %\> | &nbsp; | \<%= chapter.description %\> |

Thank you very very much. With your assistance, it came out the way I  
wanted.
