Category - Subcategory

Hi there,

I have a Products table. Each product has a category_id and
subcategory_id which relate to the categories and subcategories tables

  1. How do I create a view for creating new subcategories? Obviously,
    upon entering a new subcategory, I would need to select the category
    to which it should belong.

  2. Same problem with entering new products. I have to choose a
    category from a drop-down list and then a subcategory that relates to
    that category.

Any help greatly appreciated.

God bless,


I personally favor having a ‘tree-like’ category structure instead of
separate categories/subcategories. When you think about it, a
‘Category’ and a ‘SubCategory’ are normally the same thing - just one
is a parent of the other.

Try looking at ‘acts_as_tree’ for your categories. In this set up,
each category can have a parent_id which tells you which category a
sub or ‘child’ category is in.

For example:

id name parent_id

1 Books 0
2 Fiction 1
3 Non-Fiction 1

You might want to assign a book to non-fiction so the book’s
category_id becomes ‘3’. For simple category systems, I tend to use a
select box like this:


As for managing the categories, I tend to have the child categories
listed on my ‘Edit Category’ form. From there I can edit and create
new child categories.

Hope that helps,


Hi Steve,

Thanks for the advice. I like the tree idea and your idea for the drop-
down menu. Would you have the templates to view so I can see more
construction detail?

Much appreciated if so.

God bless,