I have stumbled upon a situation where my application looks for an id that does not exist in the database. An exception is thrown. Of course, this is a pretty standard situation
In certain cases, I would recommend that you use Model.find_by_id(id)
as opposed to Model.find(id)
. Instead of throwing an exception, .find_by_id
returns nil
. if the record could not be found.
Just make sure to check for nils to avoid NoMethodError
!
P.S. For what it's worth, Model.find_by_id(id)
is functionally equivalent to Model.where(id: id)
, which would allow you to build out some additional relations if you want.