繁体   English   中英

RoR:模型之间的关联

[英]RoR: Associations between Models

我在 2 个模型之间进行了迁移作为参考( rails g migration AddAtendimentoToPacientes atendimento:references )。

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

paciente.rb

class Paciente < ApplicationRecord
    has_many(:atendimento)
end

附注.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模型/ Atendimentos表中收集

迁移的

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

模式文件

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 指南

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

在一对多表之间添加外键时,has_many 表 (:atendimento) 将存储 (Paciente) 的 id。 实际上,您需要使用rails db rollback迁移并将迁移文件更改为以下语法。 在此处阅读有关外键的更多信息。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM