# Best place to update child an do some operations

**URL:** <https://www.ruby-forum.com/t/best-place-to-update-child-an-do-some-operations/123209>\
**Category:** Rails\
**Created:** [December 14, 2007, 11:01pm UTC](https://www.ruby-forum.com/t/best-place-to-update-child-an-do-some-operations/123209 "2007-12-14T23:01:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 14, 2007, 11:01pm UTC](https://www.ruby-forum.com/t/best-place-to-update-child-an-do-some-operations/123209/1 "2007-12-14T23:01:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 14, 2007, 11:18pm UTC](https://www.ruby-forum.com/t/best-place-to-update-child-an-do-some-operations/123209/2 "2007-12-14T23:18:05Z")

</div>

On Dec 14, 2007 4:01 PM, Raimon Fs [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 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  
need.

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 15, 2007, 9:21am UTC](https://www.ruby-forum.com/t/best-place-to-update-child-an-do-some-operations/123209/3 "2007-12-15T09:21:11Z")

</div>

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 / child.save, do a child.calculate

this child.calculate, would be:

def calcualte(child)

```
child.total = child.qt x child.price

```

end

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

thanks,

r.

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 15, 2007, 9:43am UTC](https://www.ruby-forum.com/t/best-place-to-update-child-an-do-some-operations/123209/4 "2007-12-15T09:43:02Z")

</div>

I solved it using models …

rai

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 15, 2007, 11:02am UTC](https://www.ruby-forum.com/t/best-place-to-update-child-an-do-some-operations/123209/5 "2007-12-15T11:02:35Z")

</div>

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

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  
item.

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 ?

thanks!

rai

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 / 

```

1. 

```
  item['base_calcul'] = base
  item['base_calcul_inc'] = base_inc
  item['qt'] = qty

  item['import_net'] = self.import_net

 # item['descripcio'] = 'peter'

```

end

code for parent’s controler:

if @invoice.update\_attributes(params[:invoice])

```
     params[:invoicesline].each do |key, item|
        @invoice_line=InvoicesLine.find(item[:id])

          @invoice_line.line_calculate(item)
        if @invoice_line.update_attributes(item)

        else
           #render :action => 'edit'
          end

        # @invoice.invoices_lines << InvoicesLine.new(item)
      end

```

… more code …
