Forum: Ruby on Rails error using find() method "couldn't 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.
Steven D. (Guest)
on 2006-04-11 12:57
I get the following error-message:
"Couldn't find Activitycategory with ID=1"

How do I make RoR handle this error?
When the id is nog found he should return a string or 0 or whatever.

I'm guessing by using exceptions but I'm not very familiar with ruby
itself ...

Help?

Steven.
Jean-François (Guest)
on 2006-04-11 13:16
(Received via mailing list)
Hello Steven,

> I get the following error-message:
> "Couldn't find Activitycategory with ID=1"
>
> How do I make RoR handle this error?
> When the id is nog found he should return a string or 0 or whatever.
>
> I'm guessing by using exceptions but I'm not very familiar with ruby
> itself ...

Yes, you have to catch the exception raised by the #find method :

begin
  Activitycategory.find(1)
rescue RecordNotFound
  # handle the error
  # wish a flash[] message or whatever...
end

    -- Jean-François.
This topic is locked and can not be replied to.