2017-05-16 5 views
0

私は現時点でウェブ・マーケット上に4つのテーブルを持っています。レール上のルビー・複数品目の請求書テーブル

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 

ユーザーはシリーズシーズンとチャプターのいずれかを購入することができます。主な考え方は次のとおりです。

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

どのように私はそのセリフ|シーズ|チャプターの関係を簡単に表現できますか?

答えて

1

アクティブレコードのpolymorphic associationを使用できます。

このように表現することができます。この定義に

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

あなたはこのようなテーブルが必要です。

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

t.references :containable, polymorphic: trueが整数containable_idと文字列containable_type列の両方を作成します。

+0

回答を編集して、請求書、セリフ、シーズン、チャプターテーブルの表示方法を教えてください。 – Lechucico

関連する問題