Association has_many through [RAILS 3.2]

3

I'm having problem with a has_many , through association in a project I'm working on.

In the case I need to model a relation that adds the order attribute to the relation table.

To try to model the relationship I looked for a guide in version 3.2 of the rails , because we use this version due to some adaptation problems.

I tried to implement to test the association, as in the example, but whenever I try to relate the models it does not insert the relation in the table . My code looks like this:

class Medico < ActiveRecord::Base
  attr_accessible :nome

  has_many :consultas
  has_many :pacientes, through: :consultas
end


class Paciente < ActiveRecord::Base
  attr_accessible :nome

  has_many :consultas
  has_many :medicos, through: :consultas
end

E:

class Consulta < ActiveRecord::Base

  belongs_to :medico
  belongs_to :paciente

  attr_accessible :ordem
end

The migrates were also automatically generated and looked like this:

class CreateMedicos < ActiveRecord::Migration
  def change
    create_table :medicos do |t|
      t.string :nome

      t.timestamps
    end
  end
end


class CreatePacientes < ActiveRecord::Migration
  def change
    create_table :pacientes do |t|
      t.string :nome

      t.timestamps
    end
  end
end


class CreateConsulta < ActiveRecord::Migration
  def change
    create_table :consulta do |t|
      t.references :medico
      t.references :paciente
      t.integer :ordem

      t.timestamps
    end
    add_index :consulta, :medico_id
    add_index :consulta, :paciente_id
  end
end

It turns out that when I go to create a doctor and create a patient associating with a doctor, he does not give INSERT in the query table , for example:

m1 = Medico.create(nome:"Dr. Medico") (0.4ms)  
BEGIN
  SQL (4.3ms)  INSERT INTO "medicos" ("created_at", "nome", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["created_at", Fri, 07 Dec 2018 17:28:15 -02 -02:00], ["nome", "Dr. Medico"], ["updated_at", Fri, 07 Dec 2018 17:28:15 -02 -02:00]]
   (0.6ms)  COMMIT
=> #<Medico id: 10, nome: "Dr. Medico", created_at: "2018-12-07 19:28:15", updated_at: "2018-12-07 19:28:15">

Patient.create (name: "Patient 1", doctors: [m1])

BEGIN
  SQL (0.7ms)  INSERT INTO "medicos" ("created_at", "nome", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["created_at", Fri, 07 Dec 2018 17:28:47 -02 -02:00], ["nome", "Dr. Medico"], ["updated_at", Fri, 07 Dec 2018 17:28:47 -02 -02:00]]
   (0.5ms)  COMMIT
=> #<Medico id: 11, nome: "Dr. Medico", created_at: "2018-12-07 19:28:47", updated_at: "2018-12-07 19:28:47">

Note: This is my first post on the stack, I am a developer, so I apologize right away if you get confused. :)

    
asked by anonymous 07.12.2018 / 20:36

2 answers

3

Hello, in ruby we have a more expressive way of adding items to an array.

we can use

Here, however much we use new it will save the object when added to an array.

medico = Medico.create(nome:"Dr. Medico")
medico.consultas << Paciente.new(nome:"Paciente 1")

or collection.create ()

This other way is cool because you're in the collection and ask to add another one using create .

medico = Medico.create(nome:"Dr. Medico")
medico.consultas.create(nome:"Paciente 1")
    
08.12.2018 / 14:05
0

I was able to solve only by explicitly mentioning the other model attr_accessible referencing the other model, I believe this is a limitation due to the version we are using.

    
12.12.2018 / 12:50