Polymorphic Data Modelling Question

Hello,

I am not sure of the best way to model this situation in Rails and
would appreciate some guidance.

An order can hold multiple line items. Each line item is for a
certain number of any given product. There are four products, each
in a different table because they have completely different
attributes. So I have:

Order

  • has many LineItems

LineItem

  • belongs to an Order
  • holds the quantity of the particular product
  • has one {ProductA or ProductB or ProductC or ProductD}

ProductA

  • belongs to many LineItems
  • holds various attributes unique to product A

ProductB

  • belongs to many LineItems
  • holds various attributes unique to product B

…etc for ProductC and ProductB

How do I set up LineItem so it is polymorphic with respect to the
product it has?

I know I could merge all the products into one giant product table
with the superset of all the products’ attributes, but I really don’t
want to.

What’s the best way to handle this situation?

Thanks in advance,
Andy S.

OK, I worked out how to do this. Here’s the answer to my own question:

  • holds the quantity of the particular product
    …etc for ProductC and ProductB

How do I set up LineItem so it is polymorphic with respect to the
product it has?

LineItem

  • belongs_to :orderable, ;polymorphic => true
  • column :orderable_id, :integer
  • column :orderable_type, :string

ProductA

  • has_one :line_item, :as => orderable

ProductB, ProductC, ProductD

  • as for ProductA

Regards,
Andy S.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs