基于belongs_to模型具有额外列的模型

发布于 2024-10-26 14:19:16 字数 845 浏览 2 评论 0原文

我正在建立一个产品数据库。每个产品都属于一个类别。子类别下只能有产品。每个产品都有名称、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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

于我来说 2024-11-02 14:19:16

也许您可以使用单表继承来获得您想要的。使用子类创建产品类型树,而不是为类别提供单独的类。这些子类可以定义它们允许的额外属性的验证。

例如:

产品

名为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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文