I have a Products table. Each product has a category_id and
subcategory_id which relate to the categories and subcategories tables
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.
Same problem with entering new products. I have to choose a
category from a drop-down list and then a subcategory that relates to
Any help greatly appreciated.
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.
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,
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
Much appreciated if so.