RoR:模型之间的关联

RoR: Associations between Models

我做了两个模型之间的迁移作为参考(rails g migration AddAtendimentoToPacientes atendimento:references)。

在我的模型中,我尝试使用以下关联方法:

paciente.rb

class Paciente < ApplicationRecord
    has_many(:atendimento)
end

atendimento.rb

class Atendimento < ApplicationRecord
    belongs_to(:paciente)
end

Rails 控制台输出:

[1] pry(main)> paciente = Paciente.last
  Paciente Load (0.1ms)  SELECT  "pacientes".* FROM "pacientes" ORDER BY "pacientes"."id" DESC LIMIT ?  [["LIMIT", 1]]
=> #<Paciente:0x0000000009928a20
 id: 2,
 paciente_nome: "Maria Lucia",
 paciente_cpf: "28123712283",
 paciente_idade: 25,
 created_at: Tue, 17 Nov 2020 03:30:58 UTC +00:00,
 updated_at: Tue, 17 Nov 2020 03:30:58 UTC +00:00,
 atendimento_id: nil>
[2] pry(main)> consulta = Atendimento.last
  Atendimento Load (0.1ms)  SELECT  "atendimentos".* FROM "atendimentos" ORDER BY "atendimentos"."id" DESC LIMIT ?  [["LIMIT", 1]]
=> #<Atendimento:0x000000000b4ef3a8
 id: 2,
 data_consulta: "22/12/2020",
 tipo_consulta: "Microagulhamento",
 valor_consulta: 1500.0,
 is_pago: true,
 profissional: "Rodrigo",
 created_at: Tue, 17 Nov 2020 03:30:58 UTC +00:00,
 updated_at: Tue, 17 Nov 2020 03:30:58 UTC +00:00>
[3] pry(main)> paciente.atendimento << consulta
   (0.0ms)  begin transaction
   (0.0ms)  rollback transaction
ActiveModel::MissingAttributeError: can't write unknown attribute `paciente_id`
from C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activemodel-5.2.4.4/lib/active_model/attribute.rb:207:in `with_value_from_database'

当我尝试时:

paciente = Paciente.last
consulta = Atendimento.last
paciente.atendimento << consulta

我想通知Rails,Patient模型可以在atendimento_id中有几个元素(数组),这些元素将从Atendimento模型中收集/Atendimentostable

Migration's

class CreatePacientes < ActiveRecord::Migration[5.2]
  def change
    create_table :pacientes do |t|
      t.string :paciente_nome
      t.string :paciente_cpf
      t.integer :paciente_idade

      t.timestamps
    end
  end
end
class CreateAtendimentos < ActiveRecord::Migration[5.2]
  def change
    create_table :atendimentos do |t|
      t.string :data_consulta
      t.string :tipo_consulta
      t.float :valor_consulta
      t.boolean :is_pago
      t.string :profissional

      t.timestamps
    end
  end
end
class AddAtendimentoToPacientes < ActiveRecord::Migration[5.2]
  def change
    add_reference :pacientes, :atendimento, foreign_key: true
  end
end

schema.rb

ActiveRecord::Schema.define(version: 2020_11_17_020521) do

  create_table "atendimentos", force: :cascade do |t|
    t.string "data_consulta"
    t.string "tipo_consulta"
    t.float "valor_consulta"
    t.boolean "is_pago"
    t.string "profissional"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "pacientes", force: :cascade do |t|
    t.string "paciente_nome"
    t.string "paciente_cpf"
    t.integer "paciente_idade"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "atendimento_id"
    t.index ["atendimento_id"], name: "index_pacientes_on_atendimento_id"
  end

end

在您的模型中,您可以使用以下语法建立一对多关系。参见 Rails guide

class Paciente < ApplicationRecord
    has_many :atendimento
end
class Atendimento < ApplicationRecord
    belongs_to :paciente
end

在一对多 table 之间添加外键时,has_many table (:atendimento) 将存储 (Paciente) 的 ID。实际上,您需要使用 rails db rollback 回滚迁移并将迁移文件更改为以下语法。详细了解 foreign keys here

class AddPacienteToAtendimentos < ActiveRecord::Migration[5.2]
  def change
    add_reference :atendimentos, :paciente, foreign_key: true
  end
end