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
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On Dec 14, 2007 4:01 PM, Raimon Fs <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 Donald http://destiney.com/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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.
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
I solved it using models ... rai -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 /
100)
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 ....
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---