I have the following in mind:
- A product can have multiple categories
- A category can be in different products.
- A category has a parent (category) if it's not a general category (in that case the parent would be nil)
Thinking from a relational database point of view, this would be something that I would implement as:
- Table product
- Table product_category (as primary keys: product_id, category_id)
- Table category (with a parent_id referencing a category or nil if it's a "general" cateogry)
Thinking from a Rails modelling point of view, I have the following (I avoid writing the fields that don't really matter for this relation/hierarchical problem I'm dealing):
class Product < ActiveRecord::Base
...
has_many :categories
class Category < ActiveRecord::Base
...
Here comes de doubt: How do I specify the parent_id?
Is there any way to specify that a Category has one, and just one parent ID which references to another Category?