Read more

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

Andreas Robecke
July 18, 2017Software engineer

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]
Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show archive.org snapshot

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.

Posted by Andreas Robecke to makandra dev (2017-07-18 22:48)