# Acts\_as\_tree related question

**URL:** <https://www.ruby-forum.com/t/acts-as-tree-related-question/103181>\
**Category:** Rails\
**Created:** [May 2, 2007, 7:11pm UTC](https://www.ruby-forum.com/t/acts-as-tree-related-question/103181 "2007-05-02T19:11:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![crossblaim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@crossblaim](https://www.ruby-forum.com/u/crossblaim)\
**Post date:** [May 2, 2007, 7:11pm UTC](https://www.ruby-forum.com/t/acts-as-tree-related-question/103181/1 "2007-05-02T19:11:53Z")

</div>

hi,  
I’ve a normal Category model implemented with acts\_as\_tree (name,  
parent\_id) that has\_many :products (name, category\_id).

What I’m asking for is a method to fetch every product that belongs  
fall into a particular category and its children, eg: if I’m asking  
for a root node (/category/show/1) I need to display every product  
that have category\_id = 1 AND every product of the children  
categories.

After that I must paginate results, search, but that’s really another  
story.

Somebody got an advice how to accomplish this?

thanks.

---

<div class="post-metadata">

**Author:** ![crossblaim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@crossblaim](https://www.ruby-forum.com/u/crossblaim)\
**Post date:** [May 3, 2007, 10:32am UTC](https://www.ruby-forum.com/t/acts-as-tree-related-question/103181/2 "2007-05-03T10:32:48Z")

</div>

claudio -

not that i know the specifics of your app, so this may be a useless note

- categories may be the optimized thing to do in your app, but as  
categories of products go (simple way to go) tagging is a perfect  
solution (acts\_as\_taggable) …every look into this? may be worth  
checking this out.

as what you could do for the acts\_as\_tree:

def some action  
searched\_categories = Category.find(1).children.collect … # do some  
recursive function  
@all\_associated\_products = searched\_categories.collect{ |c| c.products  
}.uniq  
end

problem with this though, is that you are querying the db a lot in a  
recursive function - a query for each “children” method.  
(##searched\_categories##). the best thing to do imho is to check out the  
acts as nested set, where then you can easily grab all of the tree of  
products with one query, and then grab the products associated… (and  
don’t forget the uniq function to remove the duplicate records…)

hth,

shai

---

<div class="post-metadata">

**Author:** ![crossblaim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@crossblaim](https://www.ruby-forum.com/u/crossblaim)\
**Post date:** [May 4, 2007, 12:40am UTC](https://www.ruby-forum.com/t/acts-as-tree-related-question/103181/3 "2007-05-04T00:40:18Z")

</div>

On May 3, 9:23 am, Claudio P. ï£¿ [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> any hints on that?
> 
> thanks again.
> 
> claudio

what about #all\_children in BNS

[http://opensource.symetrie.com/api/better\_nested\_set/classes/SymetrieCom/Acts/NestedSet/InstanceMethods.html#M000023](http://opensource.symetrie.com/api/better_nested_set/classes/SymetrieCom/Acts/NestedSet/InstanceMethods.html#M000023)

---

<div class="post-metadata">

**Author:** ![crossblaim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@crossblaim](https://www.ruby-forum.com/u/crossblaim)\
**Post date:** [May 3, 2007, 6:24pm UTC](https://www.ruby-forum.com/t/acts-as-tree-related-question/103181/4 "2007-05-03T18:24:03Z")

</div>

shai,  
thanks for your kind reply, yes, I’ve evaluated the use of tags for  
categorisation, but for now I really must stick with Category trees;  
I’ve also looked into acts\_as\_nested\_set (and the better\_nested\_set  
one), but honestly I’ve found some difficulties to manage Category in  
this way.

what I really need is some input using acts\_as\_nested\_set: eg. an  
example controller (to manage items) and the real problem: fetch every  
product in selected category and subcategories.

any hints on that?

thanks again.

claudio
