Ver Mensaje Individual
  #1 (permalink)  
Antiguo 10/03/2011, 21:52
vicman
 
Fecha de Ingreso: enero-2004
Ubicación: Medellin
Mensajes: 178
Antigüedad: 20 años, 3 meses
Puntos: 8
Pregunta last inserted id falla?

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 es el 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.categoria_tiempo = item.to_i #Categoría de hora

@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
PD: la BD es SQL server 2005

Última edición por vicman; 13/03/2011 a las 09:39