开发者

Multiple instances of a single object in a Rails Array?

G'day guys, trying to build a small invoicing system (that can generate PDF's using prawn), but am having an issue with generating multiples of indivdual items in the array开发者_Python百科. I have the Invoice class which has_many :items, but when I try to add a multiple of an item to the invoice, it won't actually add that to the invoice and it doesn't update the total.

Would I be better using a line_item model abstraction that has a has_one to the item but an integer that keeps the multiples of the item in the invoice?


It sounds like you have a many-to-many relationship, an Item may be in many invoices (more than one time) and an Invoice has many items.

class Invoice < ActiveRecord::Base
  has_many :invoice_items
  has_many :items, :through => :invoice_items
end

class InvoiceItem < ActiveRecord::Base
  belongs_to :invoice
  belongs_to :item
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜