# Handling ActiveRecord::RecordNotFound

**URL:** <https://www.ruby-forum.com/t/handling-activerecord-recordnotfound/165338>\
**Category:** Rails\
**Created:** [April 12, 2009, 6:43pm UTC](https://www.ruby-forum.com/t/handling-activerecord-recordnotfound/165338 "2009-04-12T18:43:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![amita0204](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e56c9b/32.png) [@amita0204](https://www.ruby-forum.com/u/amita0204)\
**Post date:** [April 12, 2009, 6:43pm UTC](https://www.ruby-forum.com/t/handling-activerecord-recordnotfound/165338/1 "2009-04-12T18:43:40Z")

</div>

How should we handle requests for invalid objects which do not exist in  
database, ActiveRecord::RecordNotFound error ?  
I am writing begin-rescue as follows:  
def show  
begin  
@phone = Phone.find(params[:id])  
rescue ActiveRecord::RecordNotFound  
flash[:notice] = NONEXIST\_OBJECT  
redirect\_to :controller =\> :phone, :action =\> :index  
else  
respond\_to do |format|  
format.html  
format.xml {render :xml =\> @agenda.to\_xml}  
end  
end  
end

Instead of repeating this for show, edit, and delete in every  
controller, I would like to put this into an application\_controller. How  
do I pass current controller name to the application controller? The  
controller and model names are matching, so I would generate a class  
name from controller name, then do a begin-find-rescue stuff.

Also, does this belong to controller? My first thought was to handle  
this in model, as it is related to ActiveRecord. Any thoughts on this ?

Please help me with putting it into app\_con anyway.

~  
Amita.

---

<div class="post-metadata">

**Author:** ![amita0204](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e56c9b/32.png) [@amita0204](https://www.ruby-forum.com/u/amita0204)\
**Post date:** [April 12, 2009, 6:54pm UTC](https://www.ruby-forum.com/t/handling-activerecord-recordnotfound/165338/2 "2009-04-12T18:54:53Z")

</div>

On 12 Apr 2009, at 17:43, Amita B. wrote:

> this ?

If the user asks to see object blah and that doesn’t exist, then what  
to tell the user is definitely controller material.  
If I were you I’d take a look at rescue\_from.

Fred
