1
votes

I have the current models:

  • Categories
  • SubCategories
  • Items

Each can have many of the others. For example, an item might belong to subcategory id 1, but the same item might also belong to subcategory id 2. Then subcategory id 1 might belong to category id 1, as well as subcategory id 2 belonging to category id 1.

Currently I have a HABTM relationship between each models, using a table called Categories_Sub_Categories or Items_Sub_Categories to link them. However I'd like to know is there a more efficient 'cake' way of doing this?

1

1 Answers

2
votes

Categories and SubCategories could be combined into a single "Category" model and use the Tree Behavior to keep track of which is is the parent/child of each. That also allows you to keep more than 2 levels of Categories without need for changing your code.

Then, you can just do a HABTM between Category and Item.