Rails: Why does find(id) raise an exception in rails?

Ruby on-RailsActiverecord

Ruby on-Rails Problem Overview


> Possible Duplicate:
> Model.find(1) gives ActiveRecord error when id 1 does not exist

If there is no user with an id of 1 in the database, trying User.find(1) will raise an exception.

Why is this?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Because that's the way the architects intended find(id) to work, as indicated in the RDoc:

> 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.

If you don't want the exception to be raised, use find_by_id, which will return nil if it can't find an object with the specified id. Your example would then be User.find_by_id(1).

Solution 2 - Ruby on-Rails

Further to runako's explanation, it's actually pretty useful to have the choice of whether an exception is raised or not. I'm working on a blog application and I wanted to add support for viewing the next or previous blog entry. I was able to add two instance methods to my Post model that simply return nil when you try to get the previous post when viewing the first post, or the next post when viewing the last post:

def next
  Post.find_by_id(id + 1)
end

def previous
  Post.find_by_id(id - 1)
end

This avoids my helper code which conditionally generates the Previous Post/Next Post links from having to handle the RecordNotFound exception, which would be bad because it would be using an exception for control flow.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKirschsteinView Question on Stackoverflow
Solution 1 - Ruby on-RailsrunakoView Answer on Stackoverflow
Solution 2 - Ruby on-RailsJohn TopleyView Answer on Stackoverflow