如何正确设置这些 ActiveRecord 关系?

发布于 2024-11-27 23:04:41 字数 1012 浏览 0 评论 0原文

我在理解如何为以下场景正确设置 ActiveRecord 关系时遇到问题...

现在我有这个产品树:

Product 
-> ProductTypes 
   -> Subtypes 
     -> Subtypes 
       -> ... 
         -> Subtypes 
           -> ProductItem

其中 Product 是:

class Product < ActiveRecord::Base
  has_many :product_types
  has_one :product_item, :foreign_key => "product_id"
end

ProductType 和 Subtype 是:

class ProductType < ActiveRecord::Base
  belongs_to :product
  belongs_to :parent_type, :class_name => "ProductType"
  has_many :subtypes, :class_name => "ProductType", :foreign_key => "parent_type_id"
  has_one :product_item
end

ProductItem 是:

class ProductItem < ActiveRecord::Base
  belongs_to :product_type
  belongs_to :product
end

但我也希望树允许 < code>Product 仅具有 ProductItem(即没有子类型),例如:

Product 
-> ProductItem

我该如何设置这些来实现这些要求?谢谢!

I have a problem understanding how to set up the ActiveRecord relationships correctly for the following scenarios...

Now I have this tree of products:

Product 
-> ProductTypes 
   -> Subtypes 
     -> Subtypes 
       -> ... 
         -> Subtypes 
           -> ProductItem

where Product is:

class Product < ActiveRecord::Base
  has_many :product_types
  has_one :product_item, :foreign_key => "product_id"
end

ProductType and Subtype is:

class ProductType < ActiveRecord::Base
  belongs_to :product
  belongs_to :parent_type, :class_name => "ProductType"
  has_many :subtypes, :class_name => "ProductType", :foreign_key => "parent_type_id"
  has_one :product_item
end

and ProductItem is:

class ProductItem < ActiveRecord::Base
  belongs_to :product_type
  belongs_to :product
end

But I also want the tree to allow Product to have ProductItem only (i.e. without subtypes), such as:

Product 
-> ProductItem

How can I go about setting these up to achieve these requirements? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

丢了幸福的猪 2024-12-04 23:04:41

这取决于 ProductType 是什么。如果它是一个分类,那么拥有 has_many :product_types 的关联可能是有意义的。如果您真正想要的是实际不同类型的产品,那么我会使用 STI 让您的生活变得更简单。另外,除非你有充分的理由,否则我会将product_item简化为item。

http://juixe.com/techknow/ index.php/2006/06/03/rails-single-table-inheritance/

STI 方式
添加迁移到您的产品表以添加类型列:

add_column :products, :type, :string

然后将您的模型更改为如下所示:

product.rb

class Product < ActiveRecord::Base
  has_one :item
end

type_1.rb

class Type1 < Product
end

type_2.rb

class Type2 < Product
end

等。

对于您的子类型,我会做同样的事情(以 type1 为例):

subtype_1.rb

class Subtype1 < Type1
end

所以现在所有不同的类型和子类型都有一个与其关联的项目。您的项目现在只与产品关联,您就完成了。

item.rb

class Item < ActiveRecord::Base
  belongs_to :product
end

如果这不是您想要的,那么如果您提供更清晰的信息,我将很乐意更改我的答案。

This depends on what ProductType is. If it is a categorization, then it might make sense to have that association of has_many :product_types. If what you really want are actual different types of Products, then I would use STI to make your life a little simpler. Plus I would simplify product_item to just item, unless you have good reason to.

http://juixe.com/techknow/index.php/2006/06/03/rails-single-table-inheritance/

STI Way
Add a migration to your Products table to add a type column:

add_column :products, :type, :string

Then change your models to be like the following:

product.rb

class Product < ActiveRecord::Base
  has_one :item
end

type_1.rb

class Type1 < Product
end

type_2.rb

class Type2 < Product
end

etc.

For your subtypes I would do the same thing (taking type1 for example):

subtype_1.rb

class Subtype1 < Type1
end

So now all your different types and subtypes have an item associated to them. Your item now just has an association to product and you're done.

item.rb

class Item < ActiveRecord::Base
  belongs_to :product
end

If this isn't what you want then I'll be happy to change my answer if you provide more clarity.

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