我应该如何在一个用户表中实施医生和患者?

时间:2013-09-12 13:34:08

标签: ruby-on-rails ruby data-structures devise

我想实现这些模型:

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_iddoctor_id的内容?

2 个答案:

答案 0 :(得分:1)

您的Case需要doctor_idpatient_id。这些可以引用DoctorPatient作为单独的模型:

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