0
votes

I would like to delete Patient and all their Appointments without deleting Physician.

Take the following association:

class Patient < ActiveRecord::Base
  has_many :appointments
  has_many :physicians, through: :appointments
end

class Appointment < ActiveRecord::Base
  belongs_to :physician
  belongs_to :patient
end

class Physician < ActiveRecord::Base
  has_many :appointments
  has_many :patients, through: :appointments

Every time I remove a patient, I lose all physicians that the patient has even been in contact with (via Appointment). It may be possible that the Physician has only ever seen one patient.. but that is no reason to remove the Physician when the Patient is removed.

I feel like I need something like

class Physician < ActiveRecord::Base
      has_many :appointments, dependant: hell no!

Can someone help me? Is it possible that a has_many through relationship is the wrong solution all together?

Thanks

EDIT: I would never wish to remove a Patient or Physician from the system due to an associated Patient or Physician leaving the surgery.

If a Physician leaves, this doesn't mean the patient is leaving too. Though I'm happy for their appointments to be removed. And likewise if a Patient leaves, this doesn't mean my Physician is leaving also.

1
"Note the dependent: :restrict_with_exception. This will cause Active Record to refuse to destroy any Physician records that have associated Appointment records." I would like a straight up DO NOT DESTROY. I can do it without the has_many through relationship.. but then building the form and controller is a pain in the butt.David Sigley

1 Answers

1
votes

Are you trying to delete or destroy the patients? Usually delete should skip destroying the associated records. Do check these notes in the API, though:

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#label-Delete+or+destroy%3F