product design pattern
I have the following scenario to implement: there is the product registration, but this product, obviously, has some categories. For example, it will be for male or for female, it will be an accessorie or a footwear or a bag and for example, inside the accessorie it will be a belt sub-item or wallet, depending of the wish of the admin. But I don't know how to implement this...have someone an idea of 开发者_JS百科how to this.
You want to have a many-to-many relationship between Product and Category. This means that each product can have multiple categories and each category can have multiple products.
Furthermore you want to be able to compose categories such that they have a tree-like structure. To do this, each category should reference a parent category (in the relationship schema) and/or several child categories (in code). If you want a category to be able to have multiple parent categories then you're looking at a many-to-many relationship again.
A possible alternative to Martin's suggestion is simply using tags. Tags are famous for their "flat", unstructured relationships, which could be a liability if you need "nested" categories. However, there is one tagging library, acts-as-taggable-on, which allows for nested tagging.
So instead of a flat structure like male, female, accessory, wallet, belt, you could have gender:male, gender:female, accessory:wallet, accessory:belt, etc. Logically it's the same as Martin's suggestion, but using a tagging library could save you lots of coding.
精彩评论