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

しかし、私は現在、Devise認証を使用したユーザーモデルを持っています。どのフィールドにcasesテーブルが必要ですか?それを機能させるには、関連付けで何かを変更する必要がありますか?

Doctor と User がアプリにサインインできる必要があります。

だから私はcasesテーブルが次のようなものを持つべきだpatient_idと思いますdoctor_id?

4

2 に答える 2

1

とがCase必要です。これらは、個別のモデルとしておよびを参照できます。doctor_idpatient_idDoctorPatient

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 に答える