Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

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:

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

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?

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

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
174 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.

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 Patient < ApplicationRecord
  belongs_to :doctor, class_name: 'User', foreign_key: 'doctor_id'
end

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...