How to group multiple models under the same name in unidirectional inheritance classes

(See below for a link to a sample project)

WHAT I WORK:

I have many types of users that I work with using Single Table Inheritance in Rails, for example:

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 

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

What I want to do is get all the doctors, nurses and patient staff with this call:

 @this_patient.providers # => [doctor1, nurse2, employee3] 

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

NEW FILE, I SHOULD TRY TO GET THIS WORK:

 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 

and I also added this to 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 

The problem is that I do not have a class name provider, rails throws an error:

 NoMethodError: undefined method `_reflect_on_association' for Provider:Class 

How to scan rails for viewing in Doctors, Nurses, and Employees if I call @this_patient.providers ?

EDIT

I have a sample project for work, read the instructions for reading and configure it:

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

+7
ruby-on-rails single-table-inheritance
source share
1 answer

You were very close. In your Patient model, you used β€œlike,” as if you were trying to designate it as an alias. However, the β€œhow” is used for polymorphic associations ... I replaced your Patient model with the following and was able to successfully call Patient.first.providers in the console.

 class Patient < User has_many :patient_provider_relations has_many :providers, through: :patient_provider_relations, source_type: "User" end 

Then I moved the patient service provider partner associations to concern:

 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 

And finally added include Patientable in your Doctor, Nurse, and Employee models.

+2
source share

All Articles