A
A
Andrey Demidenko2015-03-30 12:36:07
Ruby on Rails
Andrey Demidenko, 2015-03-30 12:36:07

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

1 answer(s)
V
Viktor Vsk, 2015-03-30
@Dem1

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 question

Ask a Question

731 491 924 answers to any question