How to make a table in a scaffold rails with the attributes of another scaffold

0

Personal I'm having a hard time! I created a scaffold called book and one called Loans then I created a report call in the report I want to search for books for books.

I'm using gem ransack to do searches within the same scaffold, but as in that case and within another scaffold I'm not able to

I created the scaffold reports as: rails g scaffold report book: references loan: references

    
asked by anonymous 07.06.2018 / 18:37

1 answer

0

When you use scaffold, it generates only part of the code. If you want to give more life to the software you need to write the relationships.

You need to open the Book, Loan, and Report classes and edit them, adding the relationship between them.

You can use has many through association

# model/livro.rb
class Livro < ApplicationRecord
  has_many :relatorios
  has_many :emprestimos, through: :relatorios
end

# model/relatorio.rb 
class Relatorio < ApplicationRecord
  belongs_to :livro
  belongs_to :emprestimo
end

# model/emprestimo.rb
class Emprestimo < ApplicationRecord
  has_many :relatorios
  has_many :livros, through: :relatorios
end
    
07.06.2018 / 23:00