Register two separate models in one

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 register is to select students and courses to register the class. I did the following:

Student model:

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

Course model:

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

Class model:

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

I made a schema in the class registration form where a select box appears to choose the name of the student and the name of the course:

<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 an error saying method not found 'student'

If anyone knows what I can do to arurmar, because until now I have not found anything anywhere

    
asked by anonymous 25.04.2016 / 01:14

1 answer

2

As the relationship of courses and students is of type has_many , you need to use the curso_ids and aluno_ids attribute, 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>
    
26.04.2016 / 20:45