Dehmlowm-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org Mr
2007-Aug-11 15:50 UTC
Rails exception catching
Hello I''m writting my first rails application and I would like to catch when an id cant be found. For instance have create records 1 2 and 3 I would like to: records=Record.find(4) and not get the error page rather somthing like records=Record.find(4) if (records==nil) ... do stuff ... else ... do other stuf ... end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 8/11/07, Dehmlowm-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org Mr <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hello I''m writting my first rails application and I would like to catch > when an id cant be found. > For instance have create records 1 2 and 3 > > I would like to: > records=Record.find(4) > and not get the error page rather somthing like > > records=Record.find(4) > > if (records==nil) > ... > do stuff > ... > else > ... > do other stuf > ... > endYou use rescue and read this chapter about Ruby Exceptions [1]. Good luck! [1] http://www.rubycentral.com/pickaxe/tut_exceptions.html --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Mike De wrote:> Hello I''m writting my first rails application and I would like to catch > when an id cant be found. > For instance have create records 1 2 and 3 > > I would like to: > records=Record.find(4) > and not get the error page rather somthing like > > records=Record.find(4) > > if (records==nil) > ... > do stuff > ... > else > ... > do other stuf > ... > endYou probably want something like: begin record = Record.find(14) rescue ActiveRecord::RecordNotFound puts ''Record 14 not Found!'' end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thank you both perfect. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Mike De wrote:> Hello I''m writting my first rails application and I would like to catch > when an id cant be found. > For instance have create records 1 2 and 3 > > I would like to: > records=Record.find(4) > and not get the error page rather somthing like > > records=Record.find(4) > > if (records==nil) > ... > do stuff > ... > else > ... > do other stuf > ... > endHere''s the thing: the reason that AR is throwing an exception when that fails is because you''re specifically giving it an ID. The assumption is that you have an ID, you know the record exists, and therefore if it can''t be found, it''s an exceptional situation. If the records attached to those IDs are going away for some reason, either you are stuck using a rescue, or perhaps you can refactor your problem so that you are making sure whatever dangling objects still have those ids are cleaned up before you actually try to find them. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---