Theo Graham-brown
2008-Jan-11 08:01 UTC
Dynamic Finders don''t give Exceptions? Or I can''t get any...
Hi, This has been bugging me a bit. If I do the code: begin user = User.find(:id) rescue flash[:notice] = "Invalid ID" else # rest of code end It works fine catching an invalid ID. However this code: begin user = User.find_by_email(:email) rescue flash[:notice] = "Invalid Email" else # rest of code end Never raises an exception and I have to check if ''user == nil''. This is irritating because it means my code has to be structured differently in each case. Am I doing something wrong? I''d like to have one style of error checking after every database read, for clarity. Cheers Theo -- 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 -~----------~----~----~----~------~----~------~--~---
Pat Maddox
2008-Jan-11 08:08 UTC
Re: Dynamic Finders don''t give Exceptions? Or I can''t get any...
On Jan 11, 2008 12:01 AM, Theo Graham-brown <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hi, > > This has been bugging me a bit. > > If I do the code: > > begin > user = User.find(:id) > rescue > flash[:notice] = "Invalid ID" > else > # rest of code > end > > It works fine catching an invalid ID. However this code: > > begin > user = User.find_by_email(:email) > rescue > flash[:notice] = "Invalid Email" > else > # rest of code > end > > Never raises an exception and I have to check if ''user == nil''. This is > irritating because it means my code has to be structured differently in > each case. Am I doing something wrong? I''d like to have one style of > error checking after every database read, for clarity. > > Cheers > Theo > -- > Posted via http://www.ruby-forum.com/. > > > >There''s a sidebar in AWDWR that discusses that. The gist is that if you do User.find(1), then you expect that record to exist. If you do User.find_by_some_attribute("foo"), then it''s more of a scan, and not finding a result isn''t exceptional. You can use User.find_by_id(1) if you want those same no-error semantics. I think it''s best to use whichever expresses your intent better, rather than simply trying for uniformity. Pat --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Theo Graham-brown
2008-Jan-11 08:22 UTC
Re: Dynamic Finders don''t give Exceptions? Or I can''t get any...
Pat Maddox wrote:> On Jan 11, 2008 12:01 AM, Theo Graham-brown > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> flash[:notice] = "Invalid ID" >> else >> -- >> Posted via http://www.ruby-forum.com/. >> >> > >> > > There''s a sidebar in AWDWR that discusses that. The gist is that if > you do User.find(1), then you expect that record to exist. If you do > User.find_by_some_attribute("foo"), then it''s more of a scan, and not > finding a result isn''t exceptional. > > You can use User.find_by_id(1) if you want those same no-error > semantics. I think it''s best to use whichever expresses your intent > better, rather than simply trying for uniformity. > > PatOkay, that makes sense. I''ll leave it as it is, then. I worry that I am a very poor Rails programmer, you see. With PHP/C#/C++/C/Java stuff it''s so well documentated that you can quickly see when you''re writing in a clear and correct style, but I just don''t know enough about Rails to know if I''m making a mess of things. Cheers Theo -- 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 -~----------~----~----~----~------~----~------~--~---
Pat Maddox
2008-Jan-11 08:25 UTC
Re: Dynamic Finders don''t give Exceptions? Or I can''t get any...
On Jan 11, 2008 12:22 AM, Theo Graham-brown <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Pat Maddox wrote: > > On Jan 11, 2008 12:01 AM, Theo Graham-brown > > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > >> flash[:notice] = "Invalid ID" > >> else > >> -- > >> Posted via http://www.ruby-forum.com/. > >> > >> > > >> > > > > There''s a sidebar in AWDWR that discusses that. The gist is that if > > you do User.find(1), then you expect that record to exist. If you do > > User.find_by_some_attribute("foo"), then it''s more of a scan, and not > > finding a result isn''t exceptional. > > > > You can use User.find_by_id(1) if you want those same no-error > > semantics. I think it''s best to use whichever expresses your intent > > better, rather than simply trying for uniformity. > > > > Pat > > Okay, that makes sense. I''ll leave it as it is, then. I worry that I am > a very poor Rails programmer, you see. > > With PHP/C#/C++/C/Java stuff it''s so well documentated that you can > quickly see when you''re writing in a clear and correct style, but I just > don''t know enough about Rails to know if I''m making a mess of things.Pick up a copy of Obie''s book, The Rails Way. It''s all about documenting the Rails way of doing things. Pat --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---