Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - Acts as Nested - Impossible move, target node cannot be inside moved tree

I'm using the Awesome Nested Set plugin: https://github.com/collectiveidea/awesome_nested_set

And in my model I do the following:

 acts_as_nested_set
 after_save :ensure_max_nestedset_level
private

def ensure_max_nestedset_level 
  if self.level > 2
    self.move_to_child_of(parent.parent)
  end
end

I do this to keep the levels from getting to deep. Any idea why I'm getting this "Impossible move, target node cannot be inside moved tree." error? What's strange is it happens on production but I can't replicate it on Dev.

Thanks

like image 600
AnApprentice Avatar asked Mar 08 '11 03:03

AnApprentice


1 Answers

awesome_nested_set is used to store hierarchical data structure in relation database. here are amazing article how we can store tree structure in database

http://www.sitepoint.com/hierarchical-data-database/
http://www.sitepoint.com/hierarchical-data-database-2/

awesome_nested_set also use same technique for storing tree structure in database,and in tree there should not a cyclic relation!!

So Make sure! there are no cyclic parent child relationship between nodes. awesome_nested_set check if this move can create cyclic relation it will throw exception "Impossible move, target node cannot be inside moved tree."

Example

 Food
    |\ Fruit 
    | |\Red
    | |   \Cherry 
    | |\Yellow 
    | | \Banana
    |\Meat
    | |\Beef
      |\Pork

Now in this tree you can move whole Fruit tree in child of Meet node But you can't move Fruit Node in any child of Fruit because it will make a cyclic relation and it will be impossible to iterate over tree.

Now lets comeback to your question

1-I do this to keep the levels from getting to deep You don't need to worry about levels of tree as awesome_nested_set can load whole subtree in a single sql query see Link 1 how he dose it

2- Any idea why I'm getting this "Impossible move.." error Already have explain why you are getting this error. You can prevent this error to occur by before moving any move check if this move is valid by using

move_possible?

like image 155
Naveed Avatar answered Sep 20 '22 22:09

Naveed