Rails: Why does find(id) raise an exception in rails? [duplicate]
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?
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)
.