Recursive Children Using acts_as_tree
Page 1 of 1
Recursive Children Using acts_as_tree
This is topic related to find a Recursive Children in table which has parent_id column in it.hence the element of a table which has parent_id=0 is called parent and element of a table which has parent_id= NONZERO is called children.children can also has childrens and so on....
One of the things sorely missing from Rails (and with good reason) is a function to find all the children in a tree. Below is a link that attempt at it, you can drop it in any model that acts_as_tree and call all_children to get a flattened list of all children, grandchildren, etc.
http://www.chuckvose.com/articles/2006/05/24/recursive-children
def all_children
all_children = []
all_children << list_children(self)
return all_children.flatten
end
private
def list_children(branch)
all_children = []
for child in branch.children
all_children << list_children(child)
end
if branch.children.empty?
return branch
else
all_children << branch
return all_children
end
end
i hope it brings lots of smile on a ur face
One of the things sorely missing from Rails (and with good reason) is a function to find all the children in a tree. Below is a link that attempt at it, you can drop it in any model that acts_as_tree and call all_children to get a flattened list of all children, grandchildren, etc.
http://www.chuckvose.com/articles/2006/05/24/recursive-children
def all_children
all_children = []
all_children << list_children(self)
return all_children.flatten
end
private
def list_children(branch)
all_children = []
for child in branch.children
all_children << list_children(child)
end
if branch.children.empty?
return branch
else
all_children << branch
return all_children
end
end
i hope it brings lots of smile on a ur face
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|