# Displaying nested content

**URL:** <https://www.ruby-forum.com/t/displaying-nested-content/157695>\
**Category:** Rails\
**Created:** [January 14, 2009, 2:46pm UTC](https://www.ruby-forum.com/t/displaying-nested-content/157695 "2009-01-14T14:46:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![drsharp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9bcb4/32.png) [@drsharp](https://www.ruby-forum.com/u/drsharp)\
**Post date:** [January 14, 2009, 2:46pm UTC](https://www.ruby-forum.com/t/displaying-nested-content/157695/1 "2009-01-14T14:46:27Z")

</div>

Hi, I have two tables ‘item’ and ‘comment’ (comment belonging to item).  
I am trying to list each item and the comments for that item within it.  
For example:

Item One  
Comment One  
Comment Two  
Comment Three

Item Two  
Comment One  
Comment Two

etc

I know I can place a loop within the loop to display the comments for  
each item, but that seems inefficient. From what I have read, the  
:include option in the controller can be used to carry out what I want,  
but I am unsure of how to display this information within the view.

Controller

@items = Item.find(:all, :include =\> [:comments] )

View

\<% for item in @items %\>  
\<%=h item.title %\>  
\<%=h item.body %\>

(WOULD IT BE SOMETHING LIKE THIS?)  
\<% for comment in @items.comments %\>  
COMMENT GOES HERE  
\<% end %\>

\<% end %\>

Any help would be greatly appreciated, thanks.

---

<div class="post-metadata">

**Author:** ![drsharp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9bcb4/32.png) [@drsharp](https://www.ruby-forum.com/u/drsharp)\
**Post date:** [January 14, 2009, 7:53pm UTC](https://www.ruby-forum.com/t/displaying-nested-content/157695/2 "2009-01-14T19:53:12Z")

</div>

Be brave, experiment!

Once you have an item, that item has comments, not the collection of  
items (@items)

\<% @items.each do |item| %\>  
\<%=h item.title %\>  
\<%=h item.body %\>  
\<% item.comments.each do |comment| %\>  
\<%=h comment.text (or whatever) %\>  
\<% end %\>  
\<% end %\>
