# Rails 2.0 rescue\_from

**URL:** <https://www.ruby-forum.com/t/rails-2-0-rescue-from/123949>\
**Category:** Rails\
**Created:** [December 22, 2007, 3:56pm UTC](https://www.ruby-forum.com/t/rails-2-0-rescue-from/123949 "2007-12-22T15:56:49Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [December 22, 2007, 3:56pm UTC](https://www.ruby-forum.com/t/rails-2-0-rescue-from/123949/1 "2007-12-22T15:56:49Z")

</div>

I am trying to use the new Rails 2.0 macro : rescue\_from

class PostsController \< ApplicationController  
rescue\_from ActiveRecord::RecordNotFound, :with =\> :deny\_access  
…  
def show  
@post = Post.find\_by\_id(params[:id])  
raise ActiveRecord::RecordNotFound if @post.nil? #illegal access  
…  
end

def deny\_access  
respond\_to do |format|  
format.html  
end  
end

but the exception is not raised … did I miss something ?

thanks

kad

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [December 22, 2007, 5:23pm UTC](https://www.ruby-forum.com/t/rails-2-0-rescue-from/123949/2 "2007-12-22T17:23:16Z")

</div>

On Dec 22, 2007, at 3:56 PM, Kad K. wrote:

> class PostsController \< ApplicationController  
> rescue\_from ActiveRecord::RecordNotFound, :with =\> :deny\_access  
> …  
> def show  
> @post = Post.find\_by\_id(params[:id])  
> raise ActiveRecord::RecordNotFound if @post.nil? #illegal access

That can be rewritten in a single line, since AR::Base.find precisely  
raises ActiveRecord::RecordNotFound if the record is not found:

@post = Post.find(params[:id])

> def deny\_access  
> respond\_to do |format|  
> format.html  
> end  
> end
> 
> but the exception is not raised … did I miss something ?

It should work, please debug this a little more.

– fxn

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [December 22, 2007, 5:44pm UTC](https://www.ruby-forum.com/t/rails-2-0-rescue-from/123949/3 "2007-12-22T17:44:45Z")

</div>

Xavier N. wrote:

> On Dec 22, 2007, at 3:56 PM, Kad K. wrote:
> 
> > class PostsController \< ApplicationController  
> > rescue\_from ActiveRecord::RecordNotFound, :with =\> :deny\_access  
> > …  
> > def show  
> > @post = Post.find\_by\_id(params[:id])  
> > raise ActiveRecord::RecordNotFound if @post.nil? #illegal access
> 
> That can be rewritten in a single line, since AR::Base.find precisely  
> raises ActiveRecord::RecordNotFound if the record is not found:
> 
> @post = Post.find(params[:id])
> 
> > def deny\_access  
> > respond\_to do |format|  
> > format.html  
> > end  
> > end
> > 
> > but the exception is not raised … did I miss something ?
> 
> It should work, please debug this a little more.
> 
> – fxn

thanks, it should work but it doesn’t …

NoMethodError (You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.post\_type):  
/app/controllers/posts\_controller.rb:181:in `show’

def show  
@post = Post.find\_by\_id(params[:id])  
@type = @post.post\_type

the @type is evaluated… despote the fact that @post = nil  
the exception should be raised but it’s not…

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [December 22, 2007, 6:49pm UTC](https://www.ruby-forum.com/t/rails-2-0-rescue-from/123949/4 "2007-12-22T18:49:59Z")

</div>

On Dec 22, 2007, at 5:44 PM, Kad K. wrote:

> the @type is evaluated… despote the fact that @post = nil  
> the exception should be raised but it’s not…

Note that is neither your original code, nor the one I suggested.

Dynamic finders do no raise ActiveRecord::RecordNotFound, use  
AR::Base.find instead.

– fxn

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [December 22, 2007, 8:07pm UTC](https://www.ruby-forum.com/t/rails-2-0-rescue-from/123949/5 "2007-12-22T20:07:34Z")

</div>

@post = Post.find\_by\_id(params[:id])

Yeah…that doesn’t raise the ActiveRecord::RecordNotFound. The special  
“by” finders just return nil. You need to use the base find as the guy  
above suggested.

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [December 22, 2007, 10:40pm UTC](https://www.ruby-forum.com/t/rails-2-0-rescue-from/123949/6 "2007-12-22T22:40:41Z")

</div>

Xavier N. wrote:

> On Dec 22, 2007, at 5:44 PM, Kad K. wrote:
> 
> > the @type is evaluated… despote the fact that @post = nil  
> > the exception should be raised but it’s not…
> 
> Note that is neither your original code, nor the one I suggested.
> 
> Dynamic finders do no raise ActiveRecord::RecordNotFound, use  
> AR::Base.find instead.
> 
> – fxn

OK… thanks  
sorry, I did not use exceptions until now… so I did not notice it  
I just tested in teh console :

> > Post.find(999)  
> > ActiveRecord::RecordNotFound: Couldn’t find Post with ID=999  
> > from  
> > /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.0.1/lib/active\_record/base.rb:1194:in  
> > `find_one' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.0.1/lib/active_record/base.rb:1177:in`find\_from\_ids’  
> > from  
> > /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.0.1/lib/active\_record/base.rb:453:in  
> > `find' from /Users/yves/Sites/presdemoi.net/TRY-yves-1.0/vendor/plugins/geokit/lib/geo_kit/acts_as_mappable.rb:109:in`find’  
> > from (irb):1

> > Post.find\_by\_id(999)  
> > =\> nil

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [December 22, 2007, 10:41pm UTC](https://www.ruby-forum.com/t/rails-2-0-rescue-from/123949/7 "2007-12-22T22:41:11Z")

</div>

Nathan E. wrote:

> @post = Post.find\_by\_id(params[:id])
> 
> Yeah…that doesn’t raise the ActiveRecord::RecordNotFound. The special  
> “by” finders just return nil. You need to use the base find as the guy  
> above suggested.

Thanks Nathan ;-))

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [December 23, 2007, 2:43pm UTC](https://www.ruby-forum.com/t/rails-2-0-rescue-from/123949/8 "2007-12-23T14:43:27Z")

</div>

On Dec 23, 5:40 am, Kad K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.0.1/lib/active\_record/base .rb:1194:in  
> from (irb):1
> 
> > > Post.find\_by\_id(999)
> 
> =\> nil
> 
> –  
> Posted viahttp://www.ruby-forum.com/.  
> if you want find\_by to raise, can overwrite your method:

def find\_by\_something(something)  
super || raise(ActiveRecord::RecordNotFound, ‘your own error  
message’)  
end

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [December 23, 2007, 2:50pm UTC](https://www.ruby-forum.com/t/rails-2-0-rescue-from/123949/9 "2007-12-23T14:50:28Z")

</div>

if you want find\_by to raise, can overwrite your class method:

def self.find\_by\_something(something)  
super || raise(ActiveRecord::RecordNotFound, ‘your own error  
message’)  
end

On Dec 23, 5:40 am, Kad K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
