How to group together multiple models under one name in Single Table Inheritance classes

ach*_*322 8 ruby-on-rails single-table-inheritance

(See below for link to sample project)

WHAT I HAVE WORKING:

I have many user types which I am handling using Single Table Inheritance in Rails, e.g.:

class User < ActiveRecord::Base
  self.inheritance_column = :meta_type
  scope :doctors, -> { where(meta_type: 'Doctor') }
  scope :patients, -> { where(meta_type: 'Patient') }
  scope :nurses, -> { where(meta_type: 'Nurse') }
  scope :employees, -> { where(meta_type: 'Employee') }

end

class Doctor < User
  has_many :doctor_patient_relations
  has_many :patients, :through => :doctor_patient_relations
  has_many :doctor_nurse_relations
  has_many :nurses, :through => :doctor_nurse_relations
  ...
  # More join tables between each type of user
end

class Patient < User
  has_many :doctor_patient_relations
  has_many :doctors, :through => :doctor_patient_relations
  has_many :nurse_patient_relations 
  has_many :nurses, :through => :nurse_patient_relations
  has_many :employee_patient_relations
  has_many :employees, :through => :employee_patient_relations
end
Run Code Online (Sandbox Code Playgroud)

In total I have 4 User types: Doctor, Nurse, Employee and Patient.

What I want to be able to do is get all of a patients' doctors, nurses, and employees with a call like this:

@this_patient.providers # => [doctor1, nurse2, employee3]
Run Code Online (Sandbox Code Playgroud)

To achieve this, I thought about removing the 3 different types of join tables between a patient and a provider ( e.g. doctor_patient_relations), and replacing them all with a single table called provider_patient_relations.

NEW FILE I ADDED TO TRY TO GET THIS WORKING:

class ProviderPatientRelation < ActiveRecord::Base
  belongs_to :provider, class_name: "User", :foreign_key => :provider_id
  belongs_to :patient, class_name: "User", :foreign_key => :patient_id
end
Run Code Online (Sandbox Code Playgroud)

and I also added this in the User.rb file:

class User < ActiveRecord::Base
  ...
  has_many :provider_patient_relations
  has_many :patients, -> { where meta_type: 'Doctor' || 'Nurse' }, :through => :provider_patient_relations, :inverse_of => :patient
  has_many :providers, -> { where meta_type: 'Patient' }, :through => :provider_patient_relations, :inverse_of => :provider
end
Run Code Online (Sandbox Code Playgroud)

The problem is, since I don't have a class name provider, rails is throwing an error:

NoMethodError: undefined method `_reflect_on_association' for Provider:Class
Run Code Online (Sandbox Code Playgroud)

How do I tell rails to look in Doctors, Nurses, and Employees if I call @this_patient.providers?

EDIT

I have a sample project to get working, check out the readme for instructions and getting it set up:

https://github.com/waleedasif322/group-user-types-example-rails

kjm*_*c13 3

你们非常亲密。在您的患者模型中,您使用了“as”,就好像您试图将其指定为别名一样。然而,“as”用于多态关联...我用以下内容替换了您的患者模型,并能够成功调用Patient.first.providers控制台。

class Patient < User
    has_many :patient_provider_relations
    has_many :providers, through: :patient_provider_relations, source_type: "User"
end
Run Code Online (Sandbox Code Playgroud)

然后,我将患者提供者关系关联转移到一个问题:

module Patientable
    extend ActiveSupport::Concern

    included do

        belongs_to :provider, polymorphic: true
        has_many :patient_provider_relations, as: :provider
        has_many :patients, through: :patient_provider_relations, source: :patient

    end
end
Run Code Online (Sandbox Code Playgroud)

最后添加include Patientable到您的医生、护士和雇员模型中。