2012-03-07 71 views
3

我想要一個具有基本信息(如名稱,說明,sku編號等)的通用產品模型。我還希望有另一個模型,它是特定類型的產品,基本上擴展了產品模型。例如:我想要有一個有顏色,大小等附加列的服裝模型。如何使用導軌擴展模型?

實現此目的的最佳實踐是什麼?我想多態或單表繼承。也許我會走錯路?

回答

5

單表繼承(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