我想要一个通用产品模型,其中包含名称、描述、sku 编号等基本信息。我还想要另一个模型,它是一种特定类型的产品,本质上是对产品模型的扩展。例如:我想要一个具有颜色、尺码等附加列的服装模型。
实现这个的最佳实践是什么?我在考虑多态性或单表继承。也许我走错了路??
最佳答案
单表继承 ( documentation ) 是一种常见的方法。另一种是使用模块实现共享功能。
这里是一个使用模块的例子。
module Product
def method_for_all_products
# ...
end
end
class Clothing < ActiveRecord::Base
include Product
def clothing_specific_method
# ...
end
end
class Furniture < ActiveRecord::Base
include Product
end
关于ruby-on-rails - 我如何扩展带有 rails 的模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9594747/