Adding subcategories to main categories

HI, I added categories to my restaurant review demo following the features library videos. Now i was trying to add subcategories. I created a model of subcategories and did the association with the category. Now i want subcategories to be create when i create the main category. Then later on will add ajax to the view so that when a user select a category of a restaurant then a subcategory will come up. At the moment im mixed up on making and doing the right way of the category subcategory relationship.

Posted about 5 years ago by Maxwell
Posted about 5 years ago by Alex Yang

If you just want to specify the right category-subcategory association, then you need to make sure that subcategory 'belongs_to' category and that category 'has_many' subcategories (this code goes in your Model files).

However, the sentence I'm confused about is how subcategories can be created automatically when the main category is created. How will your app know which subcategories to create?

0
Posted about 5 years ago by Maxwell

ok the first part i got it. the second part is say I'm creating main categories, i will want to specify subcategories as well for that specific category then they will be created at once.

0
Posted about 5 years ago by Alex Yang

Unless you plan on giving normal users the ability to add their own categories/subcategories, you would need to create these yourself. If there a lot, then you should consider creating them using the Rails console (or db/seeds.rb) instead of doing it through a form on your site.

0