ClosureTree / closure_tree

Easily and efficiently make your ActiveRecord models support hierarchies
https://closuretree.github.io/closure_tree/
MIT License
1.84k stars 239 forks source link

Cannot access root or children of node within before_destroy filter #168

Open kinman-enphase opened 9 years ago

kinman-enphase commented 9 years ago

On Ruby 2.2.2 and Rails 4.0.10: Before destroying a node, I want to move its children to the root node of the tree.

before_destroy :move_children

def move_children
  self.children.update_all(parent_id: self.root.id)
end

self.root.id throws an exception because self.root is nil. If I access it from within the corresponding controller, I can reach it, but I prefer to put this logic in the model.

seuros commented 9 years ago

before_destroy :move_children , prepend: true

seuros commented 9 years ago

but the code is actually bad because that won't update the hierarchy table

mceachen commented 9 years ago

Delete the before_destroy and the default https://github.com/mceachen/closure_tree#available-options of :nullify will do what you want.

kinman-enphase commented 9 years ago

:nullify isn't what I want. I want to move the children to the parent of this tree. :nullify simply removes the parent_id, which makes the children roots rather than children of different node.

seuros commented 9 years ago

That not what he want , he want to make the root to adopt the orphans.