Topic: Best way to deal with ActiveRecord::RecordNotFound

Ok.  This is coming up a lot for me.
In most cases, it's not a big deal.
I just want to deal with it like a conditional:

def find (topic_type)
      User.find(, :include => 'topics', :conditions => ["topics.type=?", topic_type]).topics
    rescue ActiveRecord::RecordNotFound
      return nil

and then just pass it along from the controller to the view.
it knows how to deal with no results!
    my_favorites =['user'])
    @my_topics = my_favorites.find topic_type
    render :template => 'shared/topics/favorites'

But it's not working for some reason.
In most cases now I find myself checking exists?
first, which I'd rather not do for the extra database hit.

However, it tends to work more like my thinking out the problem.
And don't even get me started on rescue_action_in_public, which
only seems to work in my application_controller, not in any
specific controllers I use...

Re: Best way to deal with ActiveRecord::RecordNotFound

Ok it looks like, for one, if you use find_by_id instead of find it will return nil for no results.

Re: Best way to deal with ActiveRecord::RecordNotFound

IMO, RecordNotFound is like a null pointer exception or divide by zero exception. If your application is generating them, there is something wrong with it. So to me, the way to deal with RecordNotFound is with more and better tests, and more carefully designed apps.

Re: Best way to deal with ActiveRecord::RecordNotFound

Yes, but there are certain situations, like trying to show a list of bookmarks when you haven't saved any, that don't seem quite as catastrophic...

The other situations I'm trying to deal with are more for people accessing the URLs directly--trying to view or delete items that don't exist.  I guess in those cases it makes sense to do the equivalent of a 404 error.

With finds though, there are many cases where a nil result find is not that big of a deal.