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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
gene.tani-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-May-03 22:39 UTC
Re: acts_as_tree related question
On May 3, 9:23 am, Claudio Poli <masterk...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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. > > claudiowhat about #all_children in BNS http://opensource.symetrie.com/api/better_nested_set/classes/SymetrieCom/Acts/NestedSet/InstanceMethods.html#M000023 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---