短版,然后在细节之间的多个活动记录的关系:的Rails:两款车型
在我的应用程序的用户有两个角色,工作人员和临床医生。员工用户创建病人记录。有时候,工作人员用户会为他们自己的病人创建患者记录,有时,工作人员用户会为另一位工作人员用户作为其医生的患者创建患者记录。我的意图是在每个患者记录中包括创建记录的工作人员以及作为该患者医生的工作人员。我无法找出正确的活动记录关系来完成这项工作。
在我的患者表中,我有user_id来保存创建患者记录的职员用户的ID。在控制台中,当我创建一个病人并执行Patient.last.user时,它会返回创建该记录的用户。
在我的患者表中,我还有doctor_id来保存作为该患者医生的工作人员用户的ID。在当我创建一个病人,做Patient.last.doctor控制台我得到这个错误:
的ActiveRecord :: HasManyThroughAssociationNotFoundError:找不到关联:用户在模型病人
我成功地拯救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
医生不存在作为模型 - 它必须为此工作?有没有办法让它再次引用用户?