I have the following classes:
class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients, through: :appointments
end
class Appointment < ActiveRecord::Base
belongs_to :physician
belongs_to :patient
end
class Patient < ActiveRecord::Base
has_many :appointments
has_many :physicians, through: :appointments
end
I want only a Physician to be able to create an Appointment, but not a Patient. Please let me know how I can have this restriction at the Model level.
Thanks!
You don't need to use the association to retrieve patients appointmets. Just create a getter method for them:
class Patient < ActiveRecord::Base
def appointments
Appointment.where(patient_id: self.id)
end
end
Related
Let's say the has_many through is defined like this for a rails model.
class Physician < ApplicationRecord
has_many :appointments
has_many :patients, through: :appointments
has_many :tickets, through: :appointments #For calling **physicion.tickets**. Is this possible???
end
class Appointment < ApplicationRecord
belongs_to :physician
belongs_to :patient
has_many :tickets, as: ticketable
end
class Patient < ApplicationRecord
has_many :appointments
has_many :physicians, through: :appointments
end
This works fine. But let's add a ticket model to the appointment so each appointment can keep tickets belonging to that appointment.
class Ticket < ApplicationRecord
belongs_to :ticketable, polymorphic: true #so it can be used in other models other than appointments
end
appointment.tickets works fine. But how can we fetch all tickets for a particular physician/patient like physicion.tickets or patient.tickets?
How to write an ActiveRecord query to get the number of patients that share an appointment (physician can have more than one appointments)
class Hospital < ActiveRecord::Base
has_many :appointments
has_many :patients
has_many :physicians
end
class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients, through: :appointments
belongs_to :hospital
end
class Appointment < ActiveRecord::Base
belongs_to :physician
belongs_to :patient
belongs_to :hospital
end
class Patient < ActiveRecord::Base
has_many :appointments
has_many :physicians, through: :appointments
belongs_to :hospital
end
I can get it like Patient.first.hospital.appointments. But really need to write a clean activerecord query and also where hospital_id is not null
Thanks
From what I understand, you want to do multiple nested joins with some conditions. This is how it is done cleanly:-
Patient.joins(:appointments => [:physician,:hospital]).where(patients:{<condition_hash_for_patient>},physicians:{<condition_hash_for_physician>},hospitals:{<condition_hash_for_hospital>})
http://guides.rubyonrails.org/association_basics.html
Based on the above example, I created:
class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients, through: :appointments
end
class Appointment < ActiveRecord::Base
belongs_to :physician
belongs_to :patient
end
class Patient < ActiveRecord::Base
has_many :appointments
has_many :physicians, through: :appointments
end
Can someone guide me how can I perform a cascading delete action.
if I delete a Patient, I want all appointments for the patient deleted. Do I need to use dependent keyword somewhere ? Can someone demonstrate how to solve this.
How do I delete all appointments for a particular patient ?
Thanks in advance!
class Patient < ActiveRecord::Base
has_many :appointments, dependent: :destroy
has_many :physicians, through: :appointments
end
Patient.find(id).destroy
This should work for you. Make sure you use destroy and not delete because if you use delete you won't have the cascading effect you expect.
Update 2:
If you want to destroy all appointments but not the patient you can do this:
Patient.find(id).appointments.destroy_all
I have a table called vital_sign which belongs to a patient (the patient has multiple vital signs) and to a physician (the physician captured this vital sign), but I don't care about getting physician.vital_signs, how do I express it in rails models?
I suspect something like this:
vital_signs (belongs_to :patient, belongs_to :physician) with patient_id, physician_id
patient (has_many :vital_signs)
physician (nil)
Is this correct?
You can try this :
class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients, through: :vital_signs
end
class VitalSign < ActiveRecord::Base
belongs_to :physician
belongs_to :patient
end
class Patient < ActiveRecord::Base
has_many :appointments
has_many :physicians, through: :vital_signs
end
My setup for the models:
class Doctor < ActiveRecord::Base
has_many :appointments
has_many :patients, :through => :appointments
end
class Appointment < ActiveRecord::Base
belongs_to :doctor
belongs_to :patient
end
class Patient < ActiveRecord::Base
has_many :appointments
has_many :physicians, :through => :appointments
end
In my application LogedIn-User IS A either doctor,patient or the Admin i got understand that how Doctor and patient relationship work with appointment,But how to setup the user model and table for that
class User_type < ActiveRecord::Base
belongs_to :doctors, class_name: "USER"
belongs_to :patients, class_name: "USER"
end
I know that I am missing crucial self association here but how can i do that, or any other way to set up these models and tables for that. Thanks in advance.
class User < ActiveRecord::Base
has_one :doctor
has_one :patient
end
class Doctor < ActiveRecord::Base
belongs_to :user
has_many :appointments
has_many :patients, :through => :appointments
end
class Appointment < ActiveRecord::Base
belongs_to :physician
belongs_to :patient
end
class Patient < ActiveRecord::Base
belongs_to :user
has_many :appointments
has_many :physicians, :through => :appointments
end
Hope this will work for you, If you understand many to many relationship properly.
You have typo in here: class User_type < ActiveRecord::Base
belongs-to :doctors, class_name: "USER"
belongs_to :patients, class_name: "USER"
end
it should be belongs_to, you sure thats not your problem?