Reputation: 8631
I'm making an invoicing app. The obvious model is Invoice, which has date, client_id, user_id etc. However, the Invoice can have one or more line items, which add up to the total invoice amount. Each line item has a description, rate, and quantity. Does it make sense to create another model "LineItem" or is there an easier way to just keep everything within the Invoice model? thanks!
Upvotes: 0
Views: 139
Reputation: 663
I would definitely implement an InvoiceItem model. the Items are a type of object in your program, and can be manipulated, so it's a good practice to have them separated into another table.
class Invoice << ActiveRecord::Base
has_many :invoice_items
end
class InvoiceItem << ActiveRecord::Base
belongs_to :invoice
end
Then just make sure InvoiceItem has a "invoice_id" field
Upvotes: 0
Reputation: 534
In order to normalize your database tables, you need to have two different tables as you said.Otherwise it will not fit the normalization rules of a relational database, you may check for info http://en.wikipedia.org/wiki/Database_normalization.
So, use two tables:Invoice and LineItem. And you need to put a foreign key in LineItem table, referencing Invoice table Id.
Upvotes: 1