开发者

Multiple instances of a single object in a Rails Array?

开发者 https://www.devze.com 2023-01-05 22:17 出处:网络
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

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

精彩评论

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

关注公众号