Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Triggering dependent: :destroy with overridden destroy-method

In our application we've overridden the ActiveRecord destroy method so that our records do not get deleted (so the user can undelete). Like so:

def destroy
  self.is_deleted = true
  self.save
  freeze
end

However, this seems to have disabled the dependent destroy on our has_many relationships. In other words, if destroy is called on a parent object, the child objects of has_many do not get destroyed (they don't get deleted, i.e, SQL 'DELETE...', nor is the overridden destroy-method called).

How do I trigger the destruction of the child objects.

Thanks!

like image 931
Jocke Selin Avatar asked Oct 31 '12 12:10

Jocke Selin


1 Answers

You need to trigger the destroy callback.

def destroy
  self.is_deleted = true
  self.save
  run_callbacks :destroy
  freeze
end
like image 161
vise Avatar answered Sep 28 '22 02:09

vise