基于belongs_to模型具有额外列的模型
我正在建立一个产品数据库。每个产品都属于一个类别。子类别下只能有产品。每个产品都有名称、ean_number、价格、宽度、高度、深度、重量等属性。但是还有一些基于类别的自定义属性!如能量等级、程序数量、声音音量等!我正在谈论这样的事情:
Category
|
|--Category <- This Category have extra information about extra attributes for Products
| |
| |--Category
| |
| |--Product
| |
| |--Product
|
|--Category <- This Category have extra information about extra attributes for Products
|
|--Category
| |
| |--Product
| |
| |--Product
|
|--Category
|
|--Product
|
|--Product
我不知道如何构建它。我需要一个类别和产品的数据库。但是类别需要知道要向产品添加哪些额外属性。所以也许我必须为 extra_attributes 制作一个表?然后在category和extra_attributes之间建立has_many关联?但产品也需要了解这种关联!也许我的想法完全错误......!也许我应该制作不同的产品类型?希望您能指导一下!!
I am building a products database. Each product belongs to a category. There can only be products under sub-sub-categories. Each Product have attributes like name, ean_number, price, width, height, depth, weight etc. But then there are some custom attributes based on the category! Like energy_class, number_of_programs, sound_volume, etc! I am talking about something like this:
Category
|
|--Category <- This Category have extra information about extra attributes for Products
| |
| |--Category
| |
| |--Product
| |
| |--Product
|
|--Category <- This Category have extra information about extra attributes for Products
|
|--Category
| |
| |--Product
| |
| |--Product
|
|--Category
|
|--Product
|
|--Product
I am not sure how to build it up. I need a database for categories and products. But then a category need to know what extra attributes to add to the product. So maybe I have to make a table for extra_attributes? And then make a has_many association between category and extra_attributes? But then a product need to know about this association too! Maybe I am thinking in a completely wrong direction...! Maybe I should make different Product types? I hope you can guide me!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您可以使用单表继承来获得您想要的。使用子类创建产品类型树,而不是为类别提供单独的类。这些子类可以定义它们允许的额外属性的验证。
例如:
产品
名为Product <产品(定义对 :name 的验证)
ByWeightNamedProduct <命名产品
ColorProduct <产品(定义对 :color 的验证)
CoolColorProduct <颜色产品
Perhaps you can use single table inheritance to get what you want. Rather than have a separate class for categories, create the tree of product types using subclasses. These subclasses can define validations for the extra attributes that they allow.
For example:
Product
NamedProduct < Product (defines validations on :name)
ByWeightNamedProduct < NamedProduct
ColorProduct < Product (defines validations on :color)
CoolColorProduct < ColorProduct