Hi all,
Beginner's question here: I've wondered, why I can't browse through "nested" models (belongs_to/has_many).
My models: (gist:291956 · GitHub)
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)
<h1>Listing people</h1>
<table>
<% @enterprises.each do |ent| %>
<tr> <th colspan="8"><%= ent.name %></th> </tr>
<% ent.branches.each do |branch| %> <tr> <th colspan="8"><%= branch.name %> (<%= branch.people.count %>)</
</tr> <tr> <th>First name</th> <th>Last name</th> <th>Sex</th> <th>Age</th> <th>Email</th> </tr>
<% 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 %></td> </tr> <% end %>
<% end %> <% end %> </table> <br />
<%= link_to 'New person', new_person_path %>