Forum: Rails-ES last inserted id falla?

Posted by Victor Manuel AG (vicman)
on 2011-03-10 15:57
Hola, ando liado con un problema que no se como resolver y spero que
ustedes me puedan ayudar pronto.

Estoy haciendo un guardado multiple en BD recorriendo un ciclo, en cada
guardado me debe devolver el id con el que qued;o guardado para
insertarlo en la siguiente tabla.

El problema es que siempre me devuelve el primer id que se guardó

Este esel ciclo

 value.each do |item|
                  if item.to_s != ''
                     log.error  "debug post id " + param_name  + " = " +
item.to_s
                     #parametros comunes
                     @time_entry             = TimeEntry.new
                     userid                  = User.current.id.to_i
                     @time_entry.project_id  = @sec
                     @time_entry.spent_on    = current_date
                     @time_entry.created_on  = current_date
                     @time_entry.updated_on  = current_date
                     @time_entry.activity_id = 8 #valor fijo
                     @time_entry.tyear       = time1.strftime("%Y")
                     @time_entry.tmonth      = time1.strftime("%m")
                     @time_entry.tweek       =
Date.civil(@time_entry.tyear, @time_entry.tmonth,
time1.strftime("%d").to_i).cweek.to_s
                     @time_entry.user_id     = userid

                     @time_entry.save

                     last_insert_id = @time_entry.id
                 end
end

last_insert_id, siempre me devuelve el primero que guardó, que estoy
haciendo mal?

Les agradeceria una ayuda urgente.

Un saludo
Posted by Sarah Allen (ultrasaurus)
on 2011-03-18 20:13
No puedo ver porque last_insert_id siempre se devuelve el primero que 
guardó, pero tengo algunas sugestiónes.  Si TimeEntry es un ActiveRecord 
model, hay updated_at y created_at en general. Probablemente quiere 
verificar el resulto de @time_entry.save, y puede especificar todo la 
información por TimeEntry.new como eso...

value.each do |item|
    if item.to_s != ''
        log.error  "debug post id " + param_name  + " = " +
item.to_s
        #parametros comunes
        tweek = Date.civil(@time_entry.tyear, @time_entry.tmonth, 
time1.strftime("%d").to_i).cweek.to_s;

        @time_entry = TimeEntry.new(:user_id => User.current.id,
                :project_id => @sec,
                :spent_on => current_date,
                :activity_id => 8,
                :tyear => time1.strftime("%Y"),
                :tmonth =>  time1.strftime("%m"),
                :tweek => tweek)
        if @time_entry.save
          last_insert_id = @time_entry.id
        else
          log.error  "..."
        end
    end
end

Puede buscar la problema con last_insert_id con la addicíon de log.error 
en el ciclo.

Espero de ayude,

Sarah
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.