# Browsing nested models

**URL:** <https://www.ruby-forum.com/t/browsing-nested-models/182745>\
**Category:** Rails\
**Created:** [February 2, 2010, 9:35am UTC](https://www.ruby-forum.com/t/browsing-nested-models/182745 "2010-02-02T09:35:49Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jonas](https://www.ruby-forum.com/u/jonas)\
**Post date:** [February 2, 2010, 9:35am UTC](https://www.ruby-forum.com/t/browsing-nested-models/182745/1 "2010-02-02T09:35:49Z")

</div>

Hi all,

Beginner’s question here: I’ve wondered, why I can’t browse through  
“nested” models (belongs\_to/has\_many).

My models: ([http://gist.github.com/291956](http://gist.github.com/291956))

class Enterprise \< ActiveRecord::Base  
has\_many :branches  
has\_many :people, :through =\> :branches  
end

class Branch \< ActiveRecord::Base  
has\_many :people  
belongs\_to :enterprise  
end

class Person \< ActiveRecord::Base  
belongs\_to :branch, :include =\> :enterprise  
end

And finally viewing people: ([index.html.erb · GitHub](http://gist.github.com/291953))

# Listing people

\<% @enterprises.each do |ent| %\>

\<% ent.branches.each do |branch| %\>

  
  
  
  
  
  
  
  

```
<% branch.people.each do |person| %>
<tr>
    <td><%=h person.first_name %></td>
    <td><%=h person.last_name %></td>
    <td><%=h person.sex %></td>
    <td><%=h person.age %></td>
    <td><%=h person.email %></td>
    <td><%= link_to 'Show', person %></td>
    <td><%= link_to 'Edit', edit_person_path(person) %></td>
    <td><%= link_to 'Destroy', person, :confirm => 'Are you

```

sure?', :method =\> :delete %\>  
  
\<% end %\>

\<% end %\>  
\<% end %\>

| \<%= ent.name %\> |
| --- |
| \<%= branch.name %\> (\<%= branch.people.count  
%\>)\</  
th\>  
 |
| --- |
| First name | Last name | Sex | Age | Email |
| --- | --- | --- | --- | --- |

  

\<%= link\_to ‘New person’, new\_person\_path %\>
