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

Asked

Viewed 80 times

0

I’m having a hard time! I created a scaffold called a book and a so-called loan after I created a so-called report in the report I want to do a search for book loans

I’m using Gem ransack to search within the same scaffold, but as in this case and within another scaffold I’m not getting it

I created the scaffold reports as follows: Rails g scaffold report book:

1 answer

0

When you use scaffolding 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 by adding the relationship between them.

You can use the 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.