def create @invoice = Invoice.new(params[:invoice]) @invoice.order_id = @session["order_id"] for item_id in params[:invoice_item][:item_id] do @invoice.invoice_items << InvoiceItem.new(:item_id => item_id) end if @invoice.save flash[:notice] = ‘Invoice was successfully created.’ redirect_to :action => ‘list’ else render :action => ‘new’ end end # Rails inserts the invoice header (the parent) # Immediately fetches currval(invoices_id_seq) to retrieve the newly created invoice_id # Uses that invoice_id number and iterates over the invoice_items inserting both the item_id and invoice_id # commits the results if successful