Forum: Rails-ES Problemas con el find

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Manuel P. (Guest)
on 2009-06-05 18:40
(Received via mailing list)
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
Trace<http://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:in
`find_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:in `report_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:in
`find_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:in
`send'
/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:in
`call_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:in
`perform_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:in
`perform_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:in
`perform_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:in
`cache'
/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:in
`send'
/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:in
`process_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:in
`process'
/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:in
`dispatch'
/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:in
`dispatch'
/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:in
`dispatch'
/usr/lib/ruby/1.8/mongrel/rails.rb:78:in `process'
/usr/lib/ruby/1.8/mongrel/rails.rb:76:in `synchronize'
/usr/lib/ruby/1.8/mongrel/rails.rb:76:in `process'
/usr/lib/ruby/1.8/mongrel.rb:618:in `process_client'
/usr/lib/ruby/1.8/mongrel.rb:617:in `each'
/usr/lib/ruby/1.8/mongrel.rb:617:in `process_client'
/usr/lib/ruby/1.8/mongrel.rb:736:in `run'
/usr/lib/ruby/1.8/mongrel.rb:736:in `initialize'
/usr/lib/ruby/1.8/mongrel.rb:736:in `new'
/usr/lib/ruby/1.8/mongrel.rb:736:in `run'
/usr/lib/ruby/1.8/mongrel.rb:720:in `initialize'
/usr/lib/ruby/1.8/mongrel.rb:720:in `new'
/usr/lib/ruby/1.8/mongrel.rb:720:in `run'
/usr/lib/ruby/1.8/mongrel/configurator.rb:271:in `run'
/usr/lib/ruby/1.8/mongrel/configurator.rb:270:in `each'
/usr/lib/ruby/1.8/mongrel/configurator.rb:270:in `run'
/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:in
`find_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:in `modifedoproc'
/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:in
`perform_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:in
`perform_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:in `measure'
/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:in
`perform_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:in
`cache'
/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:in
`perform_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:in
`process_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:in
`process'
/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:in
`handle_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:in
`synchronize'
/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:in
`dispatch_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:in `process'
/usr/lib/ruby/1.8/mongrel/rails.rb:76:in `synchronize'
/usr/lib/ruby/1.8/mongrel/rails.rb:76:in `process'
/usr/lib/ruby/1.8/mongrel.rb:618:in `process_client'
/usr/lib/ruby/1.8/mongrel.rb:617:in `each'
/usr/lib/ruby/1.8/mongrel.rb:617:in `process_client'
/usr/lib/ruby/1.8/mongrel.rb:736:in `run'
/usr/lib/ruby/1.8/mongrel.rb:736:in `initialize'
/usr/lib/ruby/1.8/mongrel.rb:736:in `new'
/usr/lib/ruby/1.8/mongrel.rb:736:in `run'
/usr/lib/ruby/1.8/mongrel.rb:720:in `initialize'
/usr/lib/ruby/1.8/mongrel.rb:720:in `new'
/usr/lib/ruby/1.8/mongrel.rb:720:in `run'
/usr/lib/ruby/1.8/mongrel/configurator.rb:271:in `run'
/usr/lib/ruby/1.8/mongrel/configurator.rb:270:in `each'
/usr/lib/ruby/1.8/mongrel/configurator.rb:270:in `run'
/usr/bin/mongrel_rails:128:in `run'
/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
Carlos B. (Guest)
on 2009-06-05 18:43
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
Juan G. (Guest)
on 2009-06-05 19:16
(Received via mailing list)
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>
Juan G. (Guest)
on 2009-06-05 19:21
(Received via mailing list)
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 ;-)

- Juan


2009/6/5 Juan G. <removed_email_address@domain.invalid>
Rafael G. (Guest)
on 2009-06-05 20:08
(Received via mailing list)
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:in 
`perform_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:in `initialize'
> /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:in 
`perform_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:in `initialize'
> /usr/bin/mongrel_rails:244|
> Lo que no logro entender es porque no entra al if @articulos.blank?
>
--
Rafa
Carlos B. (Guest)
on 2009-06-05 20:31
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 ;-)
>
> - 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.
Manuel P. (Guest)
on 2009-06-05 22:34
(Received via mailing list)
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ó:
This topic is locked and can not be replied to.