短いバージョンと詳細:Rails:2つのモデル間の複数のアクティブなレコード関係
私のアプリケーションでは、ユーザーは2つの役割、スタッフと臨床家の役割を果たします。スタッフのユーザーは患者の記録を作成します。スタッフのユーザは、自分の患者の患者記録を作成することがあり、場合によっては、別のスタッフを医師として服用している患者の患者レコードを作成することもあります。私の意図は、レコードを作成したスタッフのユーザとその患者の医師であるstafユーザの両方に、各患者のレコードに含めることです。私はこの仕事をするために適切な活発なレコード関係を見つけ出すのに苦労しています。
患者のテーブルには、患者レコードを作成したスタッフのIDを保存するuser_idがあります。コンソールで、患者を作成しPatient.last.userを実行すると、レコードを作成したユーザーが返されます。
私の患者テーブルには、その患者の医師であるスタッフユーザーのIDを保存するdoctor_idもあります。 ::
ActiveRecordのHasManyThroughAssociationNotFoundError::私は患者を作成し、Patient.last.doctorが、私はこのエラーを取得しないときコンソールで、関連を見つけることができませんでした:モデル患者
のユーザーが、私は成功しdoctor_idを保存していますが、私は医者の名前を私が欲しいと思うように取り戻すことができない。私は間違って何かを教えているのですか?それとも、私が使っているパターンを考えれば可能ですか?
詳細:
class User < ApplicationRecord
include Clearance::User
include StaffUser
include ClinicianUser
validates :first_name, :last_name, :role, presence: true
enum role: { staff: 0, clinician: 1 }
私はスタッフの懸念やアプリ/モデル/懸念の下で暮らす臨床医の懸念を持っている:
clinician_user.rb
require 'active_support/concern'
module ClinicianUser
extend ActiveSupport::Concern
included do
has_one :clinician_profile
has_many :lists
has_many :universities, through: :lists
after_create :create_clinician_profile
end
class_methods do
end
end
staff_user.rb
module StaffUser
extend ActiveSupport::Concern
included do
belongs_to :university
has_many :patients
has_many :referral_requests
validates :university_id, presence: true
end
class_methods do
end
end
ここに患者のモデルがあります:
class Patient < ApplicationRecord
belongs_to :user, -> { where role: :staff }
has_and_belongs_to_many :genders
has_and_belongs_to_many :concerns
has_and_belongs_to_many :insurances
has_and_belongs_to_many :races
has_many :referral_requests
has_one :doctor, through: :users
end
ドクターはモデルとして存在しません - これは動作する必要がありますか?もう一度ユーザーを参照する方法はありますか?