ruby-on-railscategoriesacts-as-taggable

choosing between category and then by many subcategories


I have some question, I will try to explain.

I want to build categorization, that provides such functions. I have some big category such Waste recycling equipment under that is subcategories such choppers then again I have subcategories.

I want to build categorization system that allows when I create new product chose such route to categories.

I want to know what is the best way to provide such function to my app ?

Any suggestions?

acts_as_taggable provide such functions ?

Thanks !


Solution

  • I've used the gem ancestry for similar purposes. This would at least provide support in the model layer for the tree structure that you're talking about.

    RailsCasts has done a tutorial about it.

    To play with ancestry, I would recommend using the rails console, for example like this:

    # category.rb
    
    class Category < ActiveRecord::Base
      attr_accessor :name, :parent
      has_ancestry
    end
    
    # rails console
    
    ~/Rails/CTK/jwbc[master] $ rails console
    Loading development environment (Rails 3.2.11)
    1.9.3p286 > main = Category.create(name: "Main category")
    # => created
    1.9.3p286 > sub1 = Category.create(name: "First subcategory", parent: main)
    # => created
    1.9.3p286 > sub2 = Category.create(name: "Sub-subcategory"), parent: sub1)
    # => created
    1.9.3p286 > main.children
    # => Would return sub1
    1.9.3p286 > main.descendants
    # => Returns sub1 and sub2
    1.9.3p286 > Category.at_depth(1)
    # => Returns all subcategories, in this case sub1