24 Jun 2008, 10:46pm
/dev/rails /dev/ruby
by

Comments Off

Rails: Won’t Someone Think of the Children?

I have a class, it acts_as_tree. I want to be able to delete nodes from the tree and heal the rift between grandparent and children, joining them as parent-child afterwards.

Seems like it should be pretty obvious but nothing I was coming up with was doing the trick. The node was always deleting yet so were its children. Then I googled upon this forum and the solution was found. When executing a before_destroy filter on a class that acts_as_tree, put the before filter first:

class VettingStage < ActiveRecord::Base
  before_destroy :extract_self_from_chain
  acts_as_tree :foreign_key => "parent_id", :o rder => "name"
  ...
end

That was easy.