(Vedi sotto per il link di assaggiare progetto)Come raggruppare più modelli sotto un unico nome in classi Single Table Inheritance
quello che ho LAVORO:
ho molti tipi di utente che sto movimentazione usando singolo tabella Inheritance in Rails, ad esempio:
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 totale ho 4 User
tipi: Doctor
, Nurse
, Employee
e Patient
.
Quello che voglio essere in grado di fare è ottenere tutti di un paziente medici, infermieri e dipendenti con un invito come questo:
@this_patient.providers # => [doctor1, nurse2, employee3]
Per raggiungere questo obiettivo, ho pensato di rimuovere i 3 tipi diversi di unire tabelle tra un paziente e un fornitore (es. doctor_patient_relations) e sostituirle tutte con una singola tabella chiamata provider_patient_relations.
NUOVO FILE ho aggiunto per cercare di ottenere questo funzionamento:
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
e ho anche aggiunto questo nel file User.rb:
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
Il problema è, dal momento che non lo faccio avere un provider di nomi di classe, rotaie sta gettando un errore:
NoMethodError: undefined method `_reflect_on_association' for Provider:Class
Come faccio a dire le rotaie di guardare a medici, infermieri e impiegati se Chiamo @this_patient.providers
?
EDIT
Ho un progetto di esempio per ottenere lavoro, controllare il readme per le istruzioni e ottenere da esso istituito:
https://github.com/waleedasif322/group-user-types-example-rails
è bello: ') – achabacha322