What''s the best way to handle a find that doesn''t find anything? For example. Model Foo @result = Foo.find(:first, :condition => "some condtion string").name if the condition is not met Foo.find stops the application with ''unexpected nil''. What is the best way to gracefully trap this error and return something meaningfull to the calling method? -- Best Regards, -Larry "Work, work, work...there is no satisfactory alternative." --- E.Taft Benson -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060403/3ee8982d/attachment.html
On 4/3/06, Larry Kelly <ldk2005@gmail.com> wrote:> What''s the best way to handle a find that doesn''t find anything? > > For example. > > Model Foo > > @result = Foo.find(:first, :condition => "some condtion string").name > > if the condition is not met Foo.find stops the application with ''unexpected > nil''. > > What is the best way to gracefully trap this error and return something > meaningfull to the calling method?@result = Foo.find(:first, :condition => "some condtion string") @result && @result.name # returns the name or nil Or you can raise an exception: @result = Foo.find(:first, :condition => "some condtion string") raise ActiveRecord::RecordNotFound unless @result @result.name -- Rick Olson http://techno-weenie.net