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 online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
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)