Cipher Technologies Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Recursive Children Using acts_as_tree

Go down

Recursive Children Using acts_as_tree Empty Recursive Children Using acts_as_tree

Post  Admin Wed Jul 30, 2008 12:29 pm

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 Very Happy

Admin
Admin

Posts : 3
Join date : 2008-07-29
Location : Nagpur

https://ciphertechnologies.board-directory.net

Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum