我想实现这些模型:
Doctor # maybe I should use STI Doctor < User
has_many :cases
Patient
has_many :cases
Case # each case has one pair of doctor and patient
belongs_to :doctor
belongs_to :patient
但我现在使用Devise身份验证的用户模型。哪些字段应该有cases
表,我是否需要更改关联中的某些内容以使其正常工作?
医生和用户应该能够登录该应用。
所以我认为cases
表应该有类似patient_id
和doctor_id
的内容?
答案 0 :(得分:1)
您的Case
需要doctor_id
和patient_id
。这些可以引用Doctor
和Patient
作为单独的模型:
class Doctor < ActiveRecord::Base
has_many :cases
end
class Patient < ActiveRecord::Base
has_many :cases
end
class Case < ActiveRecord::Base
belongs_to :doctor
belongs_to :patient
end
或者他们一般可以引用User
(可能带有某种role
字段):
class User < ActiveRecord::Base
has_many :doctor_cases, class_name: "Case", foreign_key: "doctor_id"
has_many :patient_cases, class_name: "Case", foreign_key: "patient_id"
end
class Case < ActiveRecord::Base
belongs_to :doctor, class_name: "User"
belongs_to :patient, class_name: "User"
end
现在一个用户可能是一个病例的医生,另一个病例的病人: - )
答案 1 :(得分:0)
我认为class_name
会起作用
class User < ActiveRecord::Base
has_many :doctors, :class_name => 'User', :foreign_key => 'doctor_id'
has_many :patients, :class_name => 'User', :foreign_key => 'patient_id'
end
class Case < ActiveRecord::Base
belongs_to :doctor, :class_name => 'User', :foreign_key => 'doctor_id'
belongs_to :patient, :class_name => 'User', :foreign_key => 'patient_id'
end