我有兩個型號,Clinician
和Patient
。 A clinician
has_many: patients
和patient
belongs_to :clinician
。連接表shared_patients
意在存儲patients
和clinicians
之間的附加關聯,因爲patient
可以被許多其他clinicians
共享,除此之外它還可以共享belongs_to
。這是通過使用has_and_belongs_to_many
關係完成的。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: ERROR: relation "shared_patients" does not exist 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
解決這個錯誤,或者在構建模型的任何意見,以獲得我想要的聯想會很棒。謝謝