Skip to main content

Rails RecordNotFound Exception Handling

Rails RecordNotFound Exception Handling ?


Rails RecordNotFound exception occurs when there is error while fetching records from database. RecordNotFound is one of the Exception Handling class provided by Rails.
1. What is RecordNotFound Exception:

It is Exception class defined in the

~/.rvm/gems/ruby-version/gems/activerecord-version/lib/active_record/errors.rb

errors.rb file which comes with your Rails.
2. When RecordNotFound Exception is thrown:

Code Snippet of file errors.rb mentioned above -

# Raised when Active Record cannot find record by given id or set of ids.
class RecordNotFound < ActiveRecordError

ActiveRecordError class is inherited from StandardError class which is subclass of Exception class. Thus ultimately Exception is superclass of StandardError class.

Sample code to reproduce the exception:

Suppose you have users table in your database, thus model User in codebase. And you try to fetch record by following code:

User.find(1234352)

And your database don't have user with id 1234352, then it will throw error as,

ActiveRecord::RecordNotFound: Couldn't find User with ID=1234352

As mentioned in above snippet, it is thrown when ActiveRecord can not find record in the database that you are trying to fetch.

Basically, record with corresponding fields in your where clause doesn't exist in the database.
3. Exception Handling for RecordNotFound Exception:

You can handle this exception as,

begin
// your code to fetch the database details from table
rescue RecordNotFound => e
// puts 'Handled RecordNotFound Exception'
end

Conclusion:

In this tutorial, we have seen what is Rails RecordNotFound exception and how to handle it in the codebase when we try to fetch records from the database.

Comments

Popular posts from this blog

Create dynamic sitemap on ruby on rails

Sitemaps are an easy way for webmasters to inform search engines about pages on their sites that are available for crawling. In its simplest form, a Sitemap is an XML file that lists URLs for a site along with additional metadata about each URL (when it was last updated, how often it usually changes, and how important it is, relative to other URLs in the site) so that search engines can more intelligently crawl the site. It’s basically a XML file describing all URLs in your page: The following example shows a Sitemap that contains just one URL and uses all optional tags. The optional tags are in italics. <?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">    <url>       <loc>http://www.example.com/</loc>       <lastmod>2005-01-01</lastmod>       <changefreq>monthly</changefreq>     ...

Omniauth Linked in Ruby On Rails

def get_linkedin_user_data      omniauth = request.env["omniauth.auth"]      dat=omniauth.extra.raw_info      linked_app_key = "xxxxxxx"      linkedin_secret_key = "yyyyyyy"      client = LinkedIn::Client.new(linked_app_key,linkedin_secret_key)      client.authorize_from_access(omniauth['credentials']['token'],omniauth['credentials']['secret'])      connections=client.connections(:fields => ["id", "first-name", "last-name","picture-url"])      uid=omniauth['uid']      token=omniauth["credentials"]["token"]      secret=omniauth["credentials"]["secret"]   #linked user data     omniauth = request.env["omniauth.auth"]      data             = omniauth.info      user_name...

Install Rvm on ubuntu

sudo apt-get install libgdbm-dev libncurses5-dev automake libtool bison libffi-dev curl -L https://get.rvm.io | bash -s stable source ~/.rvm/scripts/rvm rvm install 2.0.0-p645 rvm use 2.0.0-p645 --default ruby -v rvm gemset create rails3.2.8 rvm gemset use rails3.2.8 gem install rails -v 3.2.8