Как-то так:
# 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