Rails - acts as a nested - impossible move, the target node cannot be inside the moved tree

I am 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 so that the levels do not reach the depths. Any idea why I get this "Impossible move, target node cannot be inside a moved tree". error? How strange this happens in production, but I can't play it on Dev.

thanks

+6
ruby-on-rails ruby-on-rails-3 nested
source share
4 answers

awesome_nested_set is used to store a hierarchical data structure in a relationship database. here is a terrific article on how we can store a tree structure in a database

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

awesome_nested_set also uses the same technique to store the tree structure in the database, and the tree should not have a cyclic relation !!

So make sure! there are no cyclic parent child relationships between nodes. awesome_nested_set to check if this move can create a cyclic relation, it will throw an 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 the whole Fruit tree to the Meet node child element. But you cannot move the Fruit node to any Fruit child element because it will make a circular relation and it will be impossible to iterate through the tree.

Now back to your question

1-I do this so that the levels do not reach the depth You do not need to worry about tree levels, since awesome_nested_set can load whole subtrees in a single sql query, see Link 1, how it doses it

2 Any idea why I get this error "Impossible move .." Already explain why you get this error. You can prevent this error before moving any move check, if this action is valid with

move_possible?

+9
source share

This problem may occur due to the nil lft and rgt in the database.

Try to run

 Model.rebuild! 

in rails c This is a recalculation of all values โ€‹โ€‹associated with awesome_nested_set .

+5
source share

we have a column in our parent_id database, check that it is not zero for you.

+2
source share

Your data may be corrupted.

Make sure your lft values โ€‹โ€‹are less than your rgt values. I had this error while trying to move the sibilization (which was already in the right place) to the right of another node. I was confused until I noticed that the data was somehow corrupted.

0
source share

All Articles