Register two separate models in one

Asked

Viewed 86 times

1

Today I come with another question, but about code. Next, I am making a system to register students and courses and a screen to register classes where part of the registration is to select students and courses to register the class. I did the following:

Model student:

class Aluno < ActiveRecord::Base
  belongs_to :curso
  belongs_to :class_rom

Model course:

class Curso < ActiveRecord::Base
  has_many :alunos
  belongs_to :class_roms

Model class:

class ClassRom < ActiveRecord::Base
  has_many :alunos
  has_many :cursos

ai made a scheme in the class registration form where a select box appears to choose the student’s name and the course name:

<div class="field">
  <%= f.label :aluno_id %><br>
  <%= f.collection_select(:aluno_id, @alunos,
                                :id, :nome, :prompt => true) %>
</div>

<div class="field">
  <%= f.label :curso_id %><br>
  <%= f.collection_select(:curso_id, @cursos,
                                :id, :nome, :prompt => true) %>
</div>

<div class="actions">
  <%= f.submit %>
</div>

but at the time of registering a mistake saying method not found 'student'

if anyone knows what I can do to ruin it, because so far I haven’t found anything anywhere

1 answer

2

How the relationship of courses and students is of the type has_many, you need to use the attribution curso_ids and aluno_ids, so your form would look like this:

<div class="field">
  <%= f.label :aluno_ids %><br>
  <%= f.select(:aluno_ids,
               @alunos.map { |aluno| [aluno.id, aluno.nome] },
               {},
               multiple: true %>
</div>

<div class="field">
  <%= f.label :curso_id %><br>
  <%= f.select(:curso_ids,
               @cursos.map { |curso| [curso.id, curso.nome] },
               {},
               multiple: true %>
</div>

<div class="actions">
  <%= f.submit %>
</div>

Browser other questions tagged

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