複数の製品を持っている学生がいます。製品を選択して請求書を生成できます。これは生成関数です:
def generate
@student = @driving_school.students.find(params[:student_id])
@products = @student.products.find(params[:product_ids])
@invoice = @driving_school.invoices.build({student_id: @student.id})
@invoice.reference = @student.full_name
@invoice.products = @products
@invoice.payment_is_due_in = 14
if @invoice.save!
redirect_to @invoice
else
redirect_to @invoice
end
end
請求書には、before_create フィルターを使用して請求書を作成するときに生成される行が含まれています。
class Invoice < ActiveRecord::Base
...
before_create :copy_products_to_lines
...
private
def copy_products_to_lines
self.products.each do |product|
self.lines.build(
invoice_id: self.id,
description: product.name + ' ' + product.description,
amount: product.amount,
price: product.netto,
vat: true,
vat_rate: product.vat_rate,
undeletable: true
)
end
end
Rails 3.1.3 では、これは常に完璧に機能していました。Rails の最新の脆弱性のために 3.1.10 にアップデートしてから、このコードは壊れました。請求書は正常に生成されますが、明細行は作成されません。誰か知ってる?