When querying my DB via find such as in: @user = User.find(:first, :conditions => etc) why do I get an exception when the find is unsuccessful? I thought the regular behavior would be to just set @user to nil. can I change the way this operates? -- 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 -~----------~----~----~----~------~----~------~--~---
On 9/8/06, Mike Kogan <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > When querying my DB via find such as in: > > @user = User.find(:first, :conditions => etc) > > why do I get an exception when the find is unsuccessful? I thought the > regular behavior would be to just set @user to nil. can I change the way > this operates?Wow, you shouldn''t be getting an exception, at least not with :first. The agile book specifically points this out! (First edition, anyway; page 219 sidebar for those following at home.) Find''ing with an id SHOULD throw an exception though; are you specifying id in your conditions? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Actually it was not a :first it was a find by ID with some conditions and the conditions weren''t met although the id was present. -- 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 -~----------~----~----~----~------~----~------~--~---
But I''m still not clear. Should I have an exception handler for this condition to take control? Or should I never search unless I know it should be succesful? -- 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 -~----------~----~----~----~------~----~------~--~---
On 9/8/06, Mike Kogan <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > But I''m still not clear. Should I have an exception handler for this > condition to take control? Or should I never search unless I know it > should be succesful?If you''re finding by id, then yes, you should be handling exceptions. If you''re not finding by id, then if nothing''s there, you should handle that eventuality accordingly. So say the docs: (http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M000860) * Find by id: This can either be a specific id (1), a list of ids (1, 5, 6), or an array of ids ([5, 6, 10]). If no record can be found for all of the listed ids, then RecordNotFound will be raised. * Find first: This will return the first record matched by the options used. These options can either be specific conditions or merely an order. If no record can matched, nil is returned. * Find all: This will return all the records matched by the options used. If no records are found, an empty array is returned. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Michael Schreifels
2006-Sep-09 00:05 UTC
Re: why does unsuccesful find throw an exception?
On 9/8/06, Mike Kogan <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > But I''m still not clear. Should I have an exception handler for this > condition to take control? Or should I never search unless I know it > should be succesful?Well if your code is throwing an exception then yeah, you should handle exceptions ;-) How about a simple: begin @user = User.find(:first) rescue flash[:notice] = ''Sorry, fresh out of users!'' redirect_to index_url end That is, of course, assuming the User.find(:first) call would throw an exception if there are no users. I know it does for certain if you specify in an invalid user though... --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thank you sir. I have gone and gotten a lot smarter about Ruby and exceptions. I''m very familiar with this as an error-handling mechanism, I just did not realize Ruby in general had such a robust architecture in this regard. Since my first encounter with Ruby has been with Rails I am finding more things to be impressed about the Ruby architecture the further I go. I should also read the Find documentation a bit closer ;) -- 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 -~----------~----~----~----~------~----~------~--~---