ActiveRecord::RecordNotFound errors allow you to query the :name and :id of the model that could not be found

ActiveRecord::RecordNotFound errors provide quite meaningful error messages that can provide some insight on application details. Consider the following:

ActiveRecord::RecordNotFound: Couldn't find Organisation::Membership with 'id'=12 [WHERE "organisation_memberships"."user_id" = 1]

You should probably not simply render those error messages to the user directly. Instead you you might want to re-raise your own errors. ActiveRecord::RecordNotFound provides you with methods :model and :id where you can get information about what could actually not be found. This comes in handy when you want to explain what could not be found without revealing the insights of your code. Or maybe you simply want to provide a message that a person, who is not such an amazing ruby developer as you are, can understand.

Note: Of course you can not query for :id if you are finding records by another column.

Andreas Robecke Almost 7 years ago