This method is deprecated or moved on the latest stable version.
The last existing version (v1.2.6) is shown here.
add_child( child )
public
Adds a child to this object in the tree. If this object hasn’t been
initialized, it gets set up as a root node. Otherwise, this method will
update all of the other elements in the tree and shift them to the right,
keeping everything balanced.
# File activerecord/lib/active_record/acts/nested_set.rb, line 141
def add_child( child )
self.reload
child.reload
if child.root?
raise "Adding sub-tree isn\'t currently supported"
else
if ( (self[left_col_name] == nil) || (self[right_col_name] == nil) )
# Looks like we're now the root node! Woo
self[left_col_name] = 1
self[right_col_name] = 4
# What do to do about validation?
return nil unless self.save
child[parent_column] = self.id
child[left_col_name] = 2
child[right_col_name]= 3
return child.save
else
# OK, we need to add and shift everything else to the right
child[parent_column] = self.id
right_bound = self[right_col_name]
child[left_col_name] = right_bound
child[right_col_name] = right_bound + 1
self[right_col_name] += 2
self.class.base_class.transaction {
self.class.base_class.update_all( "#{left_col_name} = (#{left_col_name} + 2)", "#{scope_condition} AND #{left_col_name} >= #{right_bound}" )
self.class.base_class.update_all( "#{right_col_name} = (#{right_col_name} + 2)", "#{scope_condition} AND #{right_col_name} >= #{right_bound}" )
self.save
child.save
}
end
end
end