在我的Rails3模型中,我有两个类: Product,Service。我希望两者都是InventoryItem类型,因为我有另一个名为Store and Store has_many :InventoryItems的模型
这就是我想要达到的目的,但是我不确定如何在我的InventoryItem模型以及我的产品和服务模型中对其进行建模。InventoryItem应该仅仅是产品和服务继承的父类,还是应该将InventoryItem建模为产品和服务继承的类抽象。
提前感谢您的建议!
发布于 2011-03-02 23:23:46
我都不会使用,并遵循Mörre建议的让InventoryItem成为连接模型的方法:
class Store
has_many :inventory_items
has_many :products, :services, :through => :inventory_items
end
class InventoryItem
belongs_to :store
belongs_to :products, :services
end
class Product
has_many :inventory_items
has_many :stores, :through => :inventory_items
end
class Service
has_many :inventory_items
has_many :stores, :through => :inventory_items
end发布于 2011-03-02 23:11:11
就我个人而言,我不会使用继承。你为什么不直接说
has_many :services
has_many :products继承是非常昂贵的--无论是在运行时,还是在可读性方面。这个案例听起来像是一个非常基本的案例,不需要继承。您真的想让产品和服务真正从基类继承一些东西吗?您所写的内容表明您想要的只是建立关联。
https://stackoverflow.com/questions/5169372
复制相似问题