Forum: Ruby on Rails acts_as_tree related question

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
25e782eb7e799e78d781b0026fc6a4d8?d=identicon&s=25 Claudio Poli (masterkain82)
on 2007-05-02 19:11
(Received via mailing list)
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

After that I must paginate results, search, but that's really another

Somebody got an advice how to accomplish this?

4c3acdc3d93f54cc7a7281780ec8a4ee?d=identicon&s=25 shai (Guest)
on 2007-05-03 10:32
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

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...)


25e782eb7e799e78d781b0026fc6a4d8?d=identicon&s=25 Claudio Poli (masterkain82)
on 2007-05-03 18:24
(Received via mailing list)
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.

9dfe8c734b0f9b37a4e218425c0a2138?d=identicon&s=25 (Guest)
on 2007-05-04 00:40
(Received via mailing list)
On May 3, 9:23 am, Claudio Poli  <> wrote:
> any hints on that?
> thanks again.
> claudio

what about #all_children in BNS
This topic is locked and can not be replied to.