How can I extend the model using rails?

I want to have a generic product model that has basic information like name, description, sku number, etc. I also want to have another model, which is a specific type of product that extends the product model significantly. For example: I would like to have a garment model that has additional columns like color, size, etc.

What's the best practice to implement this? I mean polymorphism or single inheritance. Am I on the wrong track?

+3


source to share


1 answer


Inheriting individual tables ( documentation ) is a common approach. Another is the use of modules for collaboration.

Here is an example of using modules.



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

      

+5


source







All Articles