Results 1 to 1 of 1
Aug 10, 2007, 02:02 #1
problems in code that edit several second models in a form
I have a code that build a form that create a model (main model) and variable number of "second" models that have a many to one relationship with the first one. The problem is with the code that manage the update (for me there are customers and many contacts that belong to them):
@customer = Customer.find(params[:id])
params[:contacts].each_with_index do |value, index|
contact = @customer.contacts.find_or_initialize_by_id(params[:contacts][index.to_s]['id'])
flash[:notice] = 'Cliente aggiornato con successo'
redirect_to :action => 'index'
it works almost perfect but sometimes it messes it up but i don't understand why. The key here is the part that controls if the record is new record or not