Can I get a callback hook to destroy an object from another object?

advertisements

Rails 3.1.3 - ruby 1.9.3p194

I have 2 objects: Patient & Bill.

When Patient gets destroyed the corresponding Bill gets destroyed. Which is fine; however, these objects are also held remotely on Quickbooks and are updated through my application.

If a Bill is destroyed locally, my application deletes the object in Quickbooks. I do not destroy Patients in Quickbooks because there is most likely associated billing history stored there.

The problem arises when someone destroys a Patient locally, it calls destroy for all associated Bills, which fires the destroy method for Quickbooks Bills.

Is there a way to see if patient.destroy has been called from Bill model?


I am assuming you have something like this

class Patient < ActiveRecord::Base
  has_many :bills, dependent: :destroy
end
class Bills < ActiveRecord::Base
  belongs_to :patient
end

You could change this to

class Patient < ActiveRecord::Base
  has_many :bills, dependent: :delete_all
end

destroy has callbacks which you are using to remove bills from quickbooks.

delete has no callbacks it is just a deletion from the database straight through.

Thus if you destroy a patient it will delete all the Bills locally but will not run the callbacks to remove it from quickbooks.