【发布时间】:2015-08-08 18:08:07
【问题描述】:
我有两个模型,Clinician 和 Patient。 clinician has_many: patients 和 patient belongs_to :clinician。连接表shared_patients 旨在存储patients 和clinicians 之间的其他关联,因为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_patients”不存在 LINE 1: INSERT INTO "shared_patients" ("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