0

我想实现这些模型:

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

但我现在有用户模型和设计身份验证。哪些字段应该有cases表格,我是否需要更改关联中的某些内容才能使其正常工作?

医生和用户应该能够登录到应用程序。

所以我认为casestable 应该有类似patient_idand的东西doctor_id

4

2 回答 2

1

您的Case需求 adoctor_id和 a patient_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

现在用户在一种情况下可能是医生,在另一种情况下可能是患者:-)

于 2013-09-12T13:47:55.013 回答
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
于 2013-09-12T13:43:36.537 回答