has_many through association [RAILS 3.2]

Asked

Viewed 144 times

3

I’m having trouble with an association has_many, through on a project I’m working on.

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

To try to model the relationship I looked for a guide in version 3.2 Rails, as 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 went 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

And:

class Consulta < ActiveRecord::Base

  belongs_to :medico
  belongs_to :paciente

  attr_accessible :ordem
end

The migrates were also generated automatically and were 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’m gonna raise a doctor and raise a patient by associating with a doctor,, he does not give the INSERT in the table query, 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">

P1 = Patient.create(name:"Patient 1", physicians: [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’m a starter in the development area, so I’m sorry right away if I got confused. :)

2 answers

3

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

we can use <<

Here no matter how much we use the 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 are in Colection and asks to add one more using the create.

medico = Medico.create(nome:"Dr. Medico")
medico.consultas.create(nome:"Paciente 1")
  • Thank you for answering :) I tried to perform the operation, but it keeps giving error, but now a different error. Activerecord::Associationtypemismatch: Query(#47218530709060) expected, got Patient(#47218527660420) from usr/local/Bundle/Gems/activerecord-. 2.22.5/lib/active_record/Associations/Association.Rb:210:in `raise_on_type_mismatch'

0

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

  • I’m glad it worked out @jfranca

Browser other questions tagged

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