【问题标题】:has_and_belongs_to_many UndefinedTable: ERRORhas_and_belongs_to_many UndefinedTable: 错误
【发布时间】:2015-08-08 18:08:07
【问题描述】:

我有两个模型,ClinicianPatientclinician has_many: patientspatient belongs_to :clinician。连接表shared_patients 旨在存储patientsclinicians 之间的其他关联,因为patient 可以被许多其他clinicians 共享,除了belongs_to 之外。这是使用has_and_belongs_to_many 关系完成的。

查看模型:

class Clinician < ActiveRecord::Base
 has_many :patients
 has_and_belongs_to_many :shared_patients, join_table: 'shared_patients', class_name: 'Patient'
end

class Patient < ActiveRecord::Base
 belongs_to :clinician
 has_and_belongs_to_many :shared_clinicians, join_table: 'shared_patients', class_name: 'Clinician'
end

这就是我的表在 db 架构中的设置方式:

create_table "clinicians", force: true do |t|
  t.string  "first_name"
  t.string  "last_name"
  t.integer "user_id"
end

create_table "patients", force: true do |t|
  t.integer "clinician_id"
  t.string  "first_name"
  t.string  "last_name"
  t.integer "user_id"
end

create_table "shared_patients", id: false, force: true do |t|
  t.integer "clinician_id"
  t.integer "patient_id"
end

使用这些我想显示patient 共享的clinicians 列表。

现在我收到一个错误:

PG::UndefinedTable: 错误: 关系“shared_pa​​tients”不存在 LINE 1: INSERT INTO "shared_pa​​tients" ("clinician_id", "id", "patien...

如果我尝试在控制台中创建关系:

@shared = SharedPatient.new("id"=>1, "clinician_id"=>2526, "patient_id"=>1307) => #1, "clinician_id"=>2526, "patient_id"=>1307}>

@shared.save

任何有关解决此错误或构建模型以获得我想要的关联的建议都会很棒。谢谢

【问题讨论】:

    标签: ruby-on-rails ruby join associations has-and-belongs-to-many


    【解决方案1】:

    当你有一个has_and_belongs_to_many 时,你不能有一个连接表的类,即。你不能拥有SharedPatient,也不能像以前那样尝试使用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-18
      • 2015-07-29
      • 2015-10-30
      • 1970-01-01
      相关资源
      最近更新 更多