Lechucico
Lechucico

Reputation: 2102

Ruby on rail multiple product possibility on invoice table

I have by the moment 4 tables on my web-market.

Table User: Username + password + email.
Table Serie: Serie name + price + description
Table Season: Season name + price + description + fk(Serie)   //Each season belongs to a serie 
Table Chapter: Chapter name + price + description + fk(Season)   //Each chapter belongs to a season

The user would be able to buy either series seasons and chapters. The main idea is the following:

Table invoice: fk(user)   //User have multiple invoices
Table invoice_line: fk(invoice), fk(serie|season|chapter)   //Each invoice contains multiple products

How can I represent that serie|season|chapter relation easily?

Upvotes: 0

Views: 43

Answers (1)

DjezzzL
DjezzzL

Reputation: 845

You could use polymorphic association from Active Record.

It can be represented like this:

class InvoiceLine < ActiveRecord::Base
  belongs_to :invoice
  belongs_to :containable, polymorphic: true
end

With this definition you should have table like this:

create_table :invoice_lines do |t|
  t.references :invoice,
  t.references :containable, polymorphic: true
end

t.references :containable, polymorphic: true will create both integer containable_id and string containable_type columns.

Upvotes: 1

Related Questions