如何使用rails扩展模型?

Jak*_*cst 3 model-view-controller model ruby-on-rails

我想要一个具有基本信息的通用产品模型,如名称,描述,sku编号等.我还想要另一个模型,这是一种特定类型的产品,基本上扩展了产品模型.例如:我想要一个有颜色,大小等附加列的服装模型.

实现这个的最佳做法是什么?我在想多态或单表继承.也许我走错了路?

nic*_*des 5

单表继承(文档)是一种常见的方法.另一种方法是使用模块进行共享功能.

以下是使用模块的示例.

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
Run Code Online (Sandbox Code Playgroud)