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

Gem::LoadError (Specified 'mysql2' for database adapter, but the gem is not loaded. Add `gem 'mysql2'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).?

Gem::LoadError (Specified 'mysql2' for database adapter, but the gem is not loaded. Add `gem 'mysql2'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).) In rails?


1)change the gemfile in mysql version
 gem 'mysql2'

2)Change to mysql2 version
 gem 'mysql2','0.3.20'

Active Scaffold : Add radio buttons to your form ruby on rails ?

Active Scaffold : Add radio buttons to your form ruby on rails ?

The way to add radio buttons to your active scaffold create/update from is as shown below

#Controller

class UsersController < ApplicationController
active_scaffold :user do |config|
config.label = “Users”
config.columns = [:name, :gender]
config.columns[:gender].form_ui = :radio
config.columns[:gender].options[:options] = [['Male', '1'], ['Female','2']]
end
end

Get user accessing ip address in rails?

Get user accessing ip address rails?

request.ip ->returns the ip, whether is is a local proxy ip (localhost address) or not.
*request.remote_ip -> is smarter and gets the ip address of the client outside of local proxies.
3)If you are using apache in front of a mongrel, then remote_ip will return the source address of the request, which in this case will be local host because the Apache web server is making the request, so instead put this in your controller:

@remote_ip = request.env["HTTP_X_FORWARDED_FOR"]