Rails:两个模型之间的多个Active Record关系

时间:2023-01-12 13:26:30

Short version and then the details:

简短版本然后详细信息:

In my application users are of two roles, staff and clinician. Staff users create patient records. Sometimes a staff user would create a patient record for their own patient, and sometimes a staff user would create a patient record for a patient who has another staff user as their doctor. My intention is to include in each patient record both the staff user who created the record as well as the staf user who is that patient's doctor. I am having trouble figuring out the proper active record relationships to make this work.

在我的应用程序中,用户有两个角色,员工和临床医生。员工用户创建患者记录。有时,工作人员用户将为他们自己的患者创建患者记录,并且有时工作人员用户将为具有另一个工作人员用户作为他们的医生的患者创建患者记录。我的目的是在每个患者记录中包括创建记录的员工用户以及该患者的医生的员工用户。我无法找出正确的活动记录关系来完成这项工作。

In my patients table I have user_id to save the id of the staff user who created the patient record. In console when I create a patient and do Patient.last.user, it returns the user who created the record.

在我的患者表中,我有user_id来保存创建患者记录的员工用户的ID。在我创建患者并执行Patient.last.user时,在控制台中,它返回创建记录的用户。

In my patients table I also have doctor_id to save the id of the staff user who is that patient's doctor. In console when I create a patient and do Patient.last.doctor I get this error:

在我的病人表中,我也有doctor_id来保存该病人的医生的工作人员用户的身份。在我创建患者并且做Patient.last.doctor的控制台中我收到此错误:

ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association :users in model Patient

ActiveRecord :: HasManyThroughAssociationNotFoundError:找不到关联:模型患者中的用户

I am successfully saving the doctor_id, but I can't seem to retrieve the doctor name the way I want to. Am I dong something incorrectly, or is this not possible given the pattern I'm using?

我成功地保存了doctor_id,但我似乎无法按照我想要的方式取回医生的名字。我是不是错了,或者这是不可能的,因为我正在使用的模式?

Details:

细节:

class User < ApplicationRecord
  include Clearance::User
  include StaffUser
  include ClinicianUser

  validates :first_name, :last_name, :role, presence: true

  enum role: { staff: 0, clinician: 1 }

I have staff concerns and clinician concerns that live under app/model/concerns:

我有员工关注和临床医生的关注,生活在应用程序/模型/关注点下:

clinician_user.rb

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

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

Here is the patient model:

以下是患者模型:

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

Doctor doesn't exist as a model - must it for this to work? Is there any way for it to just reference users again?

医生不存在作为模型 - 它必须为此工作吗?有没有办法让它再次引用用户?

1 个解决方案

#1


1  

You don't need to create a separate Doctor model but you do need to change the active record association in your Patient model. Since you are saying that you have a doctor_id on the patients table, that means you should be using belongs_to :doctor instead of has_one :doctor.

您无需创建单独的Doctor模型,但您需要更改Patient模型中的活动记录关联。既然你说你在病人桌上有一个doctor_id,那就意味着你应该使用belongs_to:doctor而不是has_one:doctor。

But by doing that active record will assume you have a doctors table. Since that's not the case you'll need to add class_name and foreign_key arguments to belongs_to so active record knows how to properly generate queries:

但通过这样的活动记录将假设你有一张医生桌。由于情况并非如此,您需要将class_name和foreign_key参数添加到belongs_to,以便活动记录知道如何正确生成查询:

class Patient < ApplicationRecord
  belongs_to :doctor, class_name: 'User', foreign_key: 'doctor_id'
end

#1


1  

You don't need to create a separate Doctor model but you do need to change the active record association in your Patient model. Since you are saying that you have a doctor_id on the patients table, that means you should be using belongs_to :doctor instead of has_one :doctor.

您无需创建单独的Doctor模型,但您需要更改Patient模型中的活动记录关联。既然你说你在病人桌上有一个doctor_id,那就意味着你应该使用belongs_to:doctor而不是has_one:doctor。

But by doing that active record will assume you have a doctors table. Since that's not the case you'll need to add class_name and foreign_key arguments to belongs_to so active record knows how to properly generate queries:

但通过这样的活动记录将假设你有一张医生桌。由于情况并非如此,您需要将class_name和foreign_key参数添加到belongs_to,以便活动记录知道如何正确生成查询:

class Patient < ApplicationRecord
  belongs_to :doctor, class_name: 'User', foreign_key: 'doctor_id'
end