如何正确设置这些 ActiveRecord 关系?
我在理解如何为以下场景正确设置 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这取决于 ProductType 是什么。如果它是一个分类,那么拥有 has_many :product_types 的关联可能是有意义的。如果您真正想要的是实际不同类型的产品,那么我会使用 STI 让您的生活变得更简单。另外,除非你有充分的理由,否则我会将product_item简化为item。
http://juixe.com/techknow/ index.php/2006/06/03/rails-single-table-inheritance/
STI 方式
添加迁移到您的产品表以添加类型列:
然后将您的模型更改为如下所示:
product.rb
type_1.rb
type_2.rb
等。
对于您的子类型,我会做同样的事情(以 type1 为例):
subtype_1.rb
所以现在所有不同的类型和子类型都有一个与其关联的项目。您的项目现在只与产品关联,您就完成了。
item.rb
如果这不是您想要的,那么如果您提供更清晰的信息,我将很乐意更改我的答案。
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:
Then change your models to be like the following:
product.rb
type_1.rb
type_2.rb
etc.
For your subtypes I would do the same thing (taking type1 for example):
subtype_1.rb
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
If this isn't what you want then I'll be happy to change my answer if you provide more clarity.