0

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.

David Sigley
  • 1,126
  • 2
  • 13
  • 28
  • possible duplicate of [has\_many through association dependent destroy under condition of who called destroy](http://stackoverflow.com/questions/10039880/has-many-through-association-dependent-destroy-under-condition-of-who-called-des) – Chris G. May 19 '15 at 14:21
  • "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 May 19 '15 at 14:33

1 Answers1

1

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

mlabarca
  • 796
  • 7
  • 16
  • Currently, I'm letting the association do it. I've tried has_many :appointments, :dependent => :delete_all on both Physician and Patient, but this doesn't seem to work. – David Sigley May 19 '15 at 15:07