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