# Urgent acts\_as\_tree attributes

**URL:** <https://www.ruby-forum.com/t/urgent-acts-as-tree-attributes/127558>\
**Category:** Rails\
**Created:** [January 30, 2008, 12:19pm UTC](https://www.ruby-forum.com/t/urgent-acts-as-tree-attributes/127558 "2008-01-30T12:19:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![andre.ambrosio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@andre.ambrosio](https://www.ruby-forum.com/u/andre.ambrosio)\
**Post date:** [January 30, 2008, 12:19pm UTC](https://www.ruby-forum.com/t/urgent-acts-as-tree-attributes/127558/1 "2008-01-30T12:19:14Z")

</div>

Hello, I have a class “ve” which acts\_as\_tree and has an attribute  
“recommendations”, which is itself another model (referenced by a  
belongs\_to, has\_many relationship, a “ve” has many recommendations)

I wish I could get all recommendations for a specific node, knowing  
that this number would be the sum of its own recommendations and those  
of its children, and it’s children’s children and so on.

I think the best wat to do it is through a helper, but I get stuck wit  
the algorithm. For exemple, if I want only to get the sum of all  
recommendations present, it’s easy throgh the folowing helper (which  
even might be more performant, i guess)

def get\_recommandations\_number(node, ret)  
if node.has\_children?  
for n in node.children  
ret = get\_recommandations\_number(n, ret)  
end  
end  
ret += node.recommendations.count  
end

However, being recommendations another model, I can’t get it through  
the same algorithm.

I hope there’s an easy way, but I am not very good with ruby.

Would anybody please help me?
