def rebuild!(options = {})
      # Don't rebuild a valid tree.
      return true if valid?

      scope = lambda{ |node| {} }
      if acts_as_nested_set_options[:scope]
        scope = lambda { |node| node.nested_set_scope.options.merge(node.nested_set_scope.selector) }
      end
      indices = {}

      set_left_and_rights = lambda do |node|
        # set left
        left = (indices[scope.call(node)] += 1)
        # find
        node.nested_set_scope.where(parent_field_name => node.id).asc(left_field_name).asc(right_field_name).each { |n| set_left_and_rights.call(n) }
        # set right
        right = (indices[scope.call(node)] += 1)

        node.class.collection.find(:_id => node.id).update(
          {"$set" => {left_field_name => left, right_field_name => right}},
          {:safe => true}
        )
      end

      # Find root node(s)
      root_nodes = self.where(parent_field_name => nil).asc(left_field_name).asc(right_field_name).asc(:_id).each do |root_node|
        # setup index for this scope
        indices[scope.call(root_node)] ||= 0
        set_left_and_rights.call(root_node)
      end
    end
def rebuild!(options = {})
      
      return true if valid?

      scope = lambda{ |node| {} }
      if acts_as_nested_set_options[:scope]
        scope = lambda { |node| node.nested_set_scope.options.merge(node.nested_set_scope.selector) }
      end
      indices = {}

      set_left_and_rights = lambda do |node|
        
        left = (indices[scope.call(node)] += 1)
        
        node.nested_set_scope.where(parent_field_name => node.id).asc(left_field_name).asc(right_field_name).each { |n| set_left_and_rights.call(n) }
        
        right = (indices[scope.call(node)] += 1)

        node.class.collection.find(:_id => node.id).update(
          {"$set" => {left_field_name => left, right_field_name => right}},
          {:safe => true}
        )
      end

      
      root_nodes = self.where(parent_field_name => nil).asc(left_field_name).asc(right_field_name).asc(:_id).each do |root_node|
        
        indices[scope.call(root_node)] ||= 0
        set_left_and_rights.call(root_node)
      end
    end
def rebuild! ( options = { } ) # Don't rebuild a valid tree. return true if valid? scope = lambda { | node | { } } if acts_as_nested_set_options [ :scope ] scope = lambda { | node | node . nested_set_scope . options . merge ( node . nested_set_scope . selector ) } end indices = { } set_left_and_rights = lambda do | node | # set left left = ( indices [ scope . call ( node ) ] += 1 ) # find node . nested_set_scope . where ( parent_field_name => node . id ) . asc ( left_field_name ) . asc ( right_field_name ) . each { | n | set_left_and_rights . call ( n ) } # set right right = ( indices [ scope . call ( node ) ] += 1 ) node . class . collection . find ( :_id => node . id ) . update ( { "$set" => { left_field_name => left , right_field_name => right } } , { :safe => true } ) end # Find root node(s) root_nodes = self . where ( parent_field_name => nil ) . asc ( left_field_name ) . asc ( right_field_name ) . asc ( :_id ) . each do | root_node | # setup index for this scope indices [ scope . call ( root_node ) ] ||= 0 set_left_and_rights . call ( root_node ) end end
Rebuilds the left & rights if unset or invalid.  Also very useful for converting from acts_as_tree.
 Warning: Very expensive!
Rebuilds the left & rights if unset or invalid . Also very useful for converting from acts_as_tree . Warning : Very expensive!