Matthew Berman
Matthew Berman

Reputation: 8631

how should I implement an Invoice model?

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

Answers (2)

Joshua Scott
Joshua Scott

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

Jean
Jean

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

Related Questions