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

1

8

(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

Helvetii answered 22/7, 2015 at 20:47 Comment(0)
H
3

You were very close. In your Patient model you used 'as' as if you were trying to assign it as an alias. However 'as' is used for polymorphic associations... I replaced your Patient model with the following and was able to successfully call Patient.first.providers in a console.

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

I then moved the Patient Provider Relation associations to a 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.

Hyksos answered 7/8, 2015 at 13:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.