Robert Walker wrote: > Madison Kelly wrote: >> Can I ask a follow-up quesiton on how to set this up? I will have a >> product >> that can belong to one or many sub-catagories which in turn belong to >> one or >> many catagories. The same subcatagory name can belong to two different >> catagories (although its referring to a different group of products). >> For >> example: Fishingline (catagory) -> Long (subcatagory) and Knives >> (catagory) >> -> Long (subcatagory), where the products in both are different. > > If I follow what you're describing correctly there is a relational > database pattern for implementing Categories as you describe. The > pattern depends on a type of tree structure using a reflexive > relationship. > > Here a quick overview of the pattern and a link to a plugin that > implements it: > > categories (Table) > +----+-----------+-----------------+ > | id | parent_id | name | > +----+-----------+-----------------+ > | 1 | null | Fishing Line | > | 2 | 1 | Long | > | 3 | null | Knives | > | 4 | 3 | Long | > +----|-----------|-----------------+ > > class Category < ActiveRecord::Base > acts_as_tree :order => 'name' > end > > http://github.com/rails/acts_as_tree
Yikes! No. Adjacency lists such as acts_as_tree uses are a beautiful naïve design for tree structures -- and should be avoided at all costs. The problem is that (unless you're using Oracle, which has some proprietary extensions to its SQL syntax), you need a separate SQL query to get each level of descendants. What you should be using for this is probably a *nested-set* structure, which is made easy in Rails by the awesome_nested_set plugin. See Joe Celko's articles on nested sets -- http://dev.mysql.com/tech-resources/articles/hierarchical-data.html should get you started. Best, -- Marnen Laibow-Koser http://www.marnen.org [email protected] -- 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 [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

