(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 ...
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
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