Blog |

How to Handle ActiveRecord:: RecordNotFound in Ruby

How to Handle ActiveRecord:: RecordNotFound in Ruby
Table of Contents

ActiveRecord::RecordNotFound in Ruby is an error that occurs when an application is unable to find a record in the database that corresponds to the provided parameters.

ActiveRecord is a Ruby gem that is used to interact with databases in a Rails application. It provides an object-relational mapping (ORM) layer that allows us to work with database records as if they were Ruby objects.

When a record is requested using ActiveRecord, the gem will query the database and return the corresponding record if it exists. However, if the record cannot be found, Active Record will raise an ActiveRecord::RecordNotFound error.

When does ActiveRecord::RecordNotFound error occur?

The ActiveRecord::RecordNotFound error can occur in a number of ways. One common cause is when an application attempts to find a record by its ID and the provided ID does not exist in the database. For example, if an application attempts to find a user with an ID of 100 but there is no user with that ID in the database, Active Record will raise a RecordNotFound error.

Another common cause of this error is when an application attempts to find a record using a query that returns no results.

For example, if an application attempts to find a user with a specific email address and there is no user with that email address in the database, Active Record will raise an ActiveRecord::RecordNotFound error.

Example 1 - ActiveRecord::RecordNotFound Error

ActiveRecord::RecordNotFound in Ruby can be demonstrated with a simple example. Let's say we have a Rails application with a model called Article that corresponds to a database table called Articles. The Article model has a column called id that is used as the primary key for each record.

In our application, we have a controller action that retrieves an article from the database and displays it to the user. The action takes an id parameter and uses it to find the corresponding article in the database.

def show
  @article = Article.find(params[:id])
end

If a user navigates to the URL /articles/100 and finds no article with an ID of 100 in the database, Active Record raises an ActiveRecord::RecordNotFound error.

How to handle the ActiveRecord::RecordNotFound error

We can handle this error by using a begin-rescue block in our controller action.

Example 2 - Handling ActiveRecord::RecordNotFound error

def show
  begin
    @article = Article.find(params[:id])
  rescue ActiveRecord::RecordNotFound => e
    redirect_to '/404'
  end
end

Now, if the Article.find method raises a ActiveRecord::RecordNotFound error, the rescue block will redirect the user to the /404 URL. This can be a custom error page that informs the user that the requested article could not be found.

From Rails version 4.0 and above we can also use the optional method in the controller to handle the error, like this

Example 3 - Handling ActiveRecord::RecordNotFound error

def show
  @article = Article.find_by(id: params[:id])
  @article = Article.find(params[:id])
end

Now if the Article.find method raises an ActiveRecord::RecordNotFound error, it will return nil and Rails will automatically return a 404 status code with an error message that the record is not found.

So, in both cases, the error will be handled, and the user will not see a vague error page. Instead, they will be directed to a custom error page that informs the user that the requested resource could not be found.

In summary, ActiveRecord::NotFound in Ruby is an error message that occurs when an application is unable to find a record in the database that corresponds to the provided parameters. It can be caused by a missing ID or a query that returns no results. Developers can handle this error by using the begin-rescue block in Ruby or Rails 4.0's optional method in the controller.

Track, Analyze and Manage Errors With Rollbar

Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing Ruby errors easier than ever. Try it today!

"Rollbar allows us to go from alerting to impact analysis and resolution in a matter of minutes. Without it we would be flying blind."

Error Monitoring

Start continuously improving your code today.

Get Started Shape