Best place to update child an do some operations

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

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|

     if @invoice_line.update_attributes(item)

        # someething is wrong ...


flash[:notice] = 'Invoice was successfully updated.'
redirect_to :action => 'show', :id => @invoice

render :action => ‘edit’

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 …



On Dec 14, 2007 4:01 PM, Raimon Fs [email protected]

Well, the question is, where’s the best place to update the childs when
saving the parent & childs at the same time ?

You can watch the log files in dev mode, the timestamped SQL should
give you an idea of the time required to make the updates you are
curious about. Experiment until you have the code arrangement you

Greg D.

and it’s possible while I’m updating a child, to apply a method to it
for making some calculations before it’s saved into the database ?

I get each child from a loop, and get their values, and before
child.update /, do a child.calculate

this child.calculate, would be:

def calcualte(child) = child.qt x child.price


I tried to add this method into the child controller but I don’t know
how to call if from the parent’s child.



I solved it using models …


ok, it’s working ok, but I would like if there’s a better way to handle

so, after updating the parent record, I do a loop for each child.

I have each child in ‘item’

then I call a method (in de child’s model) from the child and pass that

in the model, I get from the item the attributes that I need for do some
calculations, after that, I update again the item with the new values
and maybe a add some new atributes that need also to be updated.

this is the expected way to handle this, or there’s a better one ?



code for child’s calculate in a model:

def line_calculate(item)

 base = item['base_calcul'].to_f
 base_inc = item['base_calcul_inc'].to_f
 qty = item['qt'].to_f

 self.preu_s_iva = (base * base_inc / 100) + base

 temp_import_net = self.preu_s_iva * qty

 self.import_net = temp_import_net - (temp_import_net * self.dte / 
  item['base_calcul'] = base
  item['base_calcul_inc'] = base_inc
  item['qt'] = qty

  item['import_net'] = self.import_net

 # item['descripcio'] = 'peter'


code for parent’s controler:

if @invoice.update_attributes(params[:invoice])

     params[:invoicesline].each do |key, item|

        if @invoice_line.update_attributes(item)

           #render :action => 'edit'

        # @invoice.invoices_lines <<

… more code …

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs