ok, here comes a general question …
I know that if we save a parent record, their childs aren’t get saved.
I also read in some threads, that this option is very expensive, and we
can’t loop for each child, we’ve just go directly for the modified ones.
As I’m starting with RoR, I’m going through a loop, and the speed is
great.
Well, the question is, where’s the best place to update the childs when
saving the parent & childs at the same time ?
here’s is my controller:
def update
@invoice = Invoice.find(params[:id])
if @invoice.update_attributes(params[:invoice])
params[:invoicesline].each do |key, item|
@invoice_line=InvoicesLine.find(item[:id])
if @invoice_line.update_attributes(item)
else
# someething is wrong ...
end
end
flash[:notice] = 'Invoice was successfully updated.'
redirect_to :action => 'show', :id => @invoice
else
render :action => ‘edit’
end
end
Now, I update the childs after updating the parent record.
Also, as this is a typical scenario of invoices and inovice_lines, when
I’m updating the lines, I would like to perform some method, where I
just calculate the new totals and make some operations.
As I’m saving the child (invoice_lines) in the invoice controller, where
and how I can call a method for updating the modified line_item, just
before sending to the database ?
I tried to create a method in invoices_line_controller, but I don’t know
how to call it from the invoice_controller. I tried this but it didn’t
work: @invoice_line.calculate Also I would like to pass as a parameter
the invoice_item there, and return it back.
i don’t know if this is possible or I’m going directly for the wrong way
…
As you can see in my example, there are no transaction or error checking
yet, I’ve taken it out for having a better vision.
I appreciate any idea or guidance …
thanks,
rai