Problemas con el find


#1

Hola a todos!
Amigos tengo un problema muy extraño con el find y que jamas me habia
ocurrido, el error me salio porque
porque coloque un id que no existe en la tabla articulos de mi BD (lo
hice para validar el sistema)
tengo este metodo en mi controller y como veran es algo secillo y
estupido

def report_stock
@articulo = Articulo.find(params[:id])
respond_to do |format|
if @articulo.blank?
flash[:error] = “El ID del Articulo indicado no esta registrado en
sistema”
return
else
format.html
end
end
end

Lo extraño es que si coloca un Id que si existe el metodo se ejecuta
excelente y me muestra mi vista sin problemas
pero si coloco un id que y sé que no existe resulta que me da el error
siguiente:

ActiveRecord::RecordNotFound in ArticulosController#report_stock

Couldn’t find Articulo with ID=555

RAILS_ROOT: /home/mwp/Proyectos MWP/sistema_adminprofit
Application Trace
http://localhost:3000/modificar_estado_proceso/555# | Framework
Trace http://localhost:3000/modificar_estado_proceso/555# | Full
Tracehttp://localhost:3000/modificar_estado_proceso/555#

/usr/lib/ruby/gems/1.8/gems/activerecord-2.1.2/lib/active_record/base.rb:1383:in
find_one' /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.2/lib/active_record/base.rb:1366:infind_from_ids’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.1.2/lib/active_record/base.rb:541:in
find' app/controllers/articulos_controller.rb:10:inreport_stock’ En esta
linea esta la instruccion ==> @articulo = Articulo.find(params[:id])
/usr/bin/mongrel_rails:128:in `run’
/usr/bin/mongrel_rails:244

/usr/lib/ruby/gems/1.8/gems/activerecord-2.1.2/lib/active_record/base.rb:1383:in
find_one' /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.2/lib/active_record/base.rb:1366:infind_from_ids’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.1.2/lib/active_record/base.rb:541:in
find' /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/base.rb:1166:insend’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/base.rb:1166:in
perform_action_without_filters' /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/filters.rb:579:incall_filters’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/filters.rb:572:in
perform_action_without_benchmark' /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/benchmarking.rb:68:inperform_action_without_rescue’
/usr/lib/ruby/1.8/benchmark.rb:293:in measure' /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/benchmarking.rb:68:inperform_action_without_rescue’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/rescue.rb:201:in
perform_action_without_caching' /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/caching/sql_cache.rb:13:inperform_action’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.1.2/lib/active_record/connection_adapters/abstract/query_cache.rb:33:in
cache' /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.2/lib/active_record/query_cache.rb:8:incache’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/caching/sql_cache.rb:12:in
perform_action' /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/base.rb:529:insend’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/base.rb:529:in
process_without_filters' /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/filters.rb:568:inprocess_without_session_management_support’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/session_management.rb:130:in
process' /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/base.rb:389:inprocess’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/dispatcher.rb:149:in
handle_request' /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/dispatcher.rb:107:indispatch’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/dispatcher.rb:104:in
synchronize' /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/dispatcher.rb:104:indispatch’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/dispatcher.rb:120:in
dispatch_cgi' /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/dispatcher.rb:35:indispatch’
/usr/lib/ruby/1.8/mongrel/rails.rb:78:in process' /usr/lib/ruby/1.8/mongrel/rails.rb:76:insynchronize’
/usr/lib/ruby/1.8/mongrel/rails.rb:76:in process' /usr/lib/ruby/1.8/mongrel.rb:618:inprocess_client’
/usr/lib/ruby/1.8/mongrel.rb:617:in each' /usr/lib/ruby/1.8/mongrel.rb:617:inprocess_client’
/usr/lib/ruby/1.8/mongrel.rb:736:in run' /usr/lib/ruby/1.8/mongrel.rb:736:ininitialize’
/usr/lib/ruby/1.8/mongrel.rb:736:in new' /usr/lib/ruby/1.8/mongrel.rb:736:inrun’
/usr/lib/ruby/1.8/mongrel.rb:720:in initialize' /usr/lib/ruby/1.8/mongrel.rb:720:innew’
/usr/lib/ruby/1.8/mongrel.rb:720:in run' /usr/lib/ruby/1.8/mongrel/configurator.rb:271:inrun’
/usr/lib/ruby/1.8/mongrel/configurator.rb:270:in each' /usr/lib/ruby/1.8/mongrel/configurator.rb:270:inrun’
/usr/lib/ruby/1.8/mongrel/command.rb:211:in `run’

/usr/lib/ruby/gems/1.8/gems/activerecord-2.1.2/lib/active_record/base.rb:1383:in
find_one' /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.2/lib/active_record/base.rb:1366:infind_from_ids’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.1.2/lib/active_record/base.rb:541:in
find' app/controllers/procesos_controller.rb:10:inmodifedoproc’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/base.rb:1166:in
send' /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/base.rb:1166:inperform_action_without_filters’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/filters.rb:579:in
call_filters' /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/filters.rb:572:inperform_action_without_benchmark’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/benchmarking.rb:68:in
perform_action_without_rescue' /usr/lib/ruby/1.8/benchmark.rb:293:inmeasure’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/benchmarking.rb:68:in
perform_action_without_rescue' /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/rescue.rb:201:inperform_action_without_caching’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/caching/sql_cache.rb:13:in
perform_action' /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.2/lib/active_record/connection_adapters/abstract/query_cache.rb:33:incache’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.1.2/lib/active_record/query_cache.rb:8:in
cache' /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/caching/sql_cache.rb:12:inperform_action’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/base.rb:529:in
send' /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/base.rb:529:inprocess_without_filters’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/filters.rb:568:in
process_without_session_management_support' /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/session_management.rb:130:inprocess’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/base.rb:389:in
process' /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/dispatcher.rb:149:inhandle_request’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/dispatcher.rb:107:in
dispatch' /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/dispatcher.rb:104:insynchronize’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/dispatcher.rb:104:in
dispatch' /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/dispatcher.rb:120:indispatch_cgi’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/dispatcher.rb:35:in
dispatch' /usr/lib/ruby/1.8/mongrel/rails.rb:78:inprocess’
/usr/lib/ruby/1.8/mongrel/rails.rb:76:in synchronize' /usr/lib/ruby/1.8/mongrel/rails.rb:76:inprocess’
/usr/lib/ruby/1.8/mongrel.rb:618:in process_client' /usr/lib/ruby/1.8/mongrel.rb:617:ineach’
/usr/lib/ruby/1.8/mongrel.rb:617:in process_client' /usr/lib/ruby/1.8/mongrel.rb:736:inrun’
/usr/lib/ruby/1.8/mongrel.rb:736:in initialize' /usr/lib/ruby/1.8/mongrel.rb:736:innew’
/usr/lib/ruby/1.8/mongrel.rb:736:in run' /usr/lib/ruby/1.8/mongrel.rb:720:ininitialize’
/usr/lib/ruby/1.8/mongrel.rb:720:in new' /usr/lib/ruby/1.8/mongrel.rb:720:inrun’
/usr/lib/ruby/1.8/mongrel/configurator.rb:271:in run' /usr/lib/ruby/1.8/mongrel/configurator.rb:270:ineach’
/usr/lib/ruby/1.8/mongrel/configurator.rb:270:in run' /usr/bin/mongrel_rails:128:inrun’
/usr/lib/ruby/1.8/mongrel/command.rb:211:in `run’
/usr/bin/mongrel_rails:244

Request

Parameters:

{“id”=>“555”}

Revise por el log y rails me crea mi sentencia sql sin problemas
(select * from articulos where articulos.id = 555)
Lo que no logro entender es porque no entra al if @articulos.blank?
Nota: He probado con @articulo.nil y nada
Alguien me puede explicar?
Gracias


#2

Manuel P. wrote:

Hola a todos!
Amigos tengo un problema muy extraño con el find y que jamas me habia
ocurrido, el error me salio porque
porque coloque un id que no existe en la tabla articulos de mi BD (lo
hice para validar el sistema)
tengo este metodo en mi controller y como veran es algo secillo y
estupido

Pues claro que es normal, no encuentra ningún registro con ese ID por
tanto lanza una excepción, cambia tu código a esto:

def report_stock
begin
@articulo = Articulo.find(params[:id])
rescue
respond_to do |format|
flash[:error] = "ID inválido
format.html {#lo que sea }
end
end
end


#3

En producción puedes tener la tranquilidad de que ese “problema” no se
transforma en un 500 sino en un cotidiano 404.

  • Juan

2009/6/5 Carlos Belizón removed_email_address@domain.invalid


#4

Por otro lado, si lo que quieres es capturar el error y tratarlo, el
camino
es el del begin-rescue pero yo sería más cauto, Carlos, poniendo un
*rescue
PonAquíTuExcepción. *Los rescue a secas son agujeros negros de errores
que
te pueden hacer perder mucho tiempo :wink:

  • Juan

2009/6/5 Juan G. removed_email_address@domain.invalid


#5

Si en vez de usar find usas find_by_id te devolverá nil en vez de una
excepción y se adaptará al flujo que has definido.

Manuel P. wrote:

flash[:error] = "El ID del Articulo indicado no esta registrado en sistema"

http://localhost:3000/modificar_estado_proceso/555# | Framework
|/usr/lib/ruby/gems/1.8/gems/activerecord-2.1.2/lib/active_record/base.rb:1383:in find_one' /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/caching/sql_cache.rb:13:inperform_action’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/dispatcher.rb:104:in synchronize' /usr/lib/ruby/1.8/mongrel.rb:736:ininitialize’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.1.2/lib/active_record/base.rb:1366:in find_from_ids' /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/caching/sql_cache.rb:13:inperform_action’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_controller/dispatcher.rb:104:in synchronize' /usr/lib/ruby/1.8/mongrel.rb:736:ininitialize’
/usr/bin/mongrel_rails:244|
Lo que no logro entender es porque no entra al if @articulos.blank?


Rafa


#6

Juan G. galisteo wrote:

Por otro lado, si lo que quieres es capturar el error y tratarlo, el
camino
es el del begin-rescue pero yo sería más cauto, Carlos, poniendo un
*rescue
PonAquíTuExcepción. *Los rescue a secas son agujeros negros de errores
que
te pueden hacer perder mucho tiempo :wink:

  • Juan

2009/6/5 Juan G. removed_email_address@domain.invalid

Cierto, pero realmente aún no he profundizado en exceso en rails como
para saber qué excepciones recoger.


#7

gracias rafael y a los demas por contestar…
les digo que la mejor solucion al problema fue usando find_by_id alli si
me
regresa nulo el resultado y puedo usar mi sentenci if @articulo.nil? sin
problemas
Gracias Rafael

El 6 de junio de 2009 12:01, Carlos Belizón <
removed_email_address@domain.invalid> escribió: