# ActiveRecord find exceptions

**URL:** <https://www.ruby-forum.com/t/activerecord-find-exceptions/81063>\
**Category:** Rails\
**Created:** [January 3, 2007, 3:19pm UTC](https://www.ruby-forum.com/t/activerecord-find-exceptions/81063 "2007-01-03T15:19:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [January 3, 2007, 3:19pm UTC](https://www.ruby-forum.com/t/activerecord-find-exceptions/81063/1 "2007-01-03T15:19:17Z")

</div>

If find myself doing something like this a lot:

Model.find(some\_criteria).field

which throws an exception if nothing is found. I am looking for a  
better way of doing something like this:

if Model.find(some\_criteria)  
Model.find(some\_criteria).field  
else  
Model.new  
end

How are other people handling situations like this?

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [January 3, 2007, 3:40pm UTC](https://www.ruby-forum.com/t/activerecord-find-exceptions/81063/2 "2007-01-03T15:40:34Z")

</div>

hi,

try

x=Model.find(some\_criteria).field || Model.new

in case you dont require Model.new can use “” as message for nothing

~gaurav

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [January 3, 2007, 3:56pm UTC](https://www.ruby-forum.com/t/activerecord-find-exceptions/81063/3 "2007-01-03T15:56:56Z")

</div>

gaurav bagga wrote:

> hi,
> 
> try
> 
> x=Model.find(some\_criteria).field || Model.new
> 
> in case you dont require Model.new can use “” as message for nothing
> 
> ~gaurav

That doesn’t work for me. I get a ActiveRecord::RecordNotFound error.

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [January 3, 2007, 4:24pm UTC](https://www.ruby-forum.com/t/activerecord-find-exceptions/81063/4 "2007-01-03T16:24:50Z")

</div>

hi,

in my previous reply I dint think of the exception hence ☹  
so apologies

i tried something

begin  
Model.find(criteria)  
rescue  
“”  
end

other thing i tried was

Model.find\_by\_sql(“select \* from models where id = 0”)

gave me empty array if entry was not there  
else array with instance of model matching criteria.

hope this helps

regards  
gaurav

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [January 3, 2007, 4:36pm UTC](https://www.ruby-forum.com/t/activerecord-find-exceptions/81063/5 "2007-01-03T16:36:52Z")

</div>

On 1/3/07, Ryan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> else  
> Model.new  
> end
> 
> How are other people handling situations like this?

if record = Record.find\_by\_id 1  
record.foo  
end

–  
Rick O.  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [January 3, 2007, 5:45pm UTC](https://www.ruby-forum.com/t/activerecord-find-exceptions/81063/6 "2007-01-03T17:45:27Z")

</div>

gaurav bagga wrote:

> “”
> 
> > > 
> > 
> > –  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> > 
> > >

You can also do this…

x=Model.find(criteria).field rescue “default value”

\_Kevin

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [January 4, 2007, 2:20pm UTC](https://www.ruby-forum.com/t/activerecord-find-exceptions/81063/7 "2007-01-04T14:20:53Z")

</div>

Sebastian D. has an interesting solution for this situations, he  
posted it at his blog:

[http://www.notsostupid.com/blog/2006/06/28/do-or-do-not-or-maybe-try/](http://www.notsostupid.com/blog/2006/06/28/do-or-do-not-or-maybe-try/)

Currently offline, but you can checkit at googles cache:

[http://64.233.161.104/search?q=cache:4KIQLnwdpLYJ:www.notsostupid.com/blog/2006/06/28/do-or-do-not-or-maybe-try/+site:www.notsostupid.com+exception&hl=en&ct=clnk&cd=1&client=firefox](http://64.233.161.104/search?q=cache:4KIQLnwdpLYJ:www.notsostupid.com/blog/2006/06/28/do-or-do-not-or-maybe-try/+site:www.notsostupid.com+exception&hl=en&ct=clnk&cd=1&client=firefox)

Basically what you do is:

module Kernel

# Returns ‘value’ in case of an exception, otherwise returns the

execution

# of the given block

def try(value = nil)  
yield if block\_given?  
rescue Exception =\> exception  
value  
end  
end

Hope this simplies your exception handling for this situations

–  
Aníbal Rojas

> **[RUBYCORNER.COM](https://www.rubycorner.com/)**
>
> RUBYCORNER.COM - Contact us for any business inquiries

> **[Home - Has Many Developers](https://hasmanydevelopers.com/)**
>
> We are the best choice for your building design and development
