Answer the question
In order to leave comments, you need to log in
How to display all contracts of this section with accounts in a table, if there are no accounts, then still display the contract in a table with empty account values?
class Invoice < ActiveRecord::Base
belongs_to :contract
end
class Contract < ActiveRecord::Base
has_many :invoices
belongs_to :department
end
class Department < ActiveRecord::Base
has_many :contracts
end
Answer the question
In order to leave comments, you need to log in
Something like this:
# 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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question