Acts_as_tree related question

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?


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



On May 3, 9:23 am, Claudio P.  [email protected] wrote:

any hints on that?

thanks again.


what about #all_children in BNS

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.


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs