# app/models/department.rb
class Department < ActiveRecord::Base
has_many :contracts
has_many :invoices, through: :contracts
end
# app/controllers/departments_controller.rb
def index
@departments = Department.includes(contracts: [:invoices]).all
end
# app/views/departments/index.html.haml
%table
%tr
%td Name
%td Contracts
= render @departments
# app/views/departments/_department.html.haml
%tr
%td= department.name
%td
= render department.contracts
# app/views/contracts/_contract.html.haml
= contract.name
- if contract.invoices.present?
= render contract.invoices
- else
No invoices on this contract.
%hr/
# app/views/invoices/_invoice.html.haml
= invoice.total_sum
<%= @questions.each do | question | %>
<% @questions.each do | question | %>
# controllers/users_controller.rb
@user = User.find(params[:id])
render 'shared/profile', resource: @user
# views/shared/_profile.html.erb
resource.name