# Dynamic Finders don't give Exceptions? Or I can't get any

**URL:** <https://www.ruby-forum.com/t/dynamic-finders-dont-give-exceptions-or-i-cant-get-any/125700>\
**Category:** Rails\
**Created:** [January 11, 2008, 9:01am UTC](https://www.ruby-forum.com/t/dynamic-finders-dont-give-exceptions-or-i-cant-get-any/125700 "2008-01-11T09:01:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![theogb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bb73d2/32.png) [@theogb](https://www.ruby-forum.com/u/theogb)\
**Post date:** [January 11, 2008, 9:01am UTC](https://www.ruby-forum.com/t/dynamic-finders-dont-give-exceptions-or-i-cant-get-any/125700/1 "2008-01-11T09:01:31Z")

</div>

Hi,

This has been bugging me a bit.

If I do the code:

begin  
user = User.find(:id)  
rescue  
flash[:notice] = “Invalid ID”  
else

# rest of code

end

It works fine catching an invalid ID. However this code:

begin  
user = User.find\_by\_email(:email)  
rescue  
flash[:notice] = “Invalid Email”  
else

# rest of code

end

Never raises an exception and I have to check if ‘user == nil’. This is  
irritating because it means my code has to be structured differently in  
each case. Am I doing something wrong? I’d like to have one style of  
error checking after every database read, for clarity.

Cheers  
Theo

---

<div class="post-metadata">

**Author:** ![theogb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bb73d2/32.png) [@theogb](https://www.ruby-forum.com/u/theogb)\
**Post date:** [January 11, 2008, 9:09am UTC](https://www.ruby-forum.com/t/dynamic-finders-dont-give-exceptions-or-i-cant-get-any/125700/2 "2008-01-11T09:09:48Z")

</div>

On Jan 11, 2008 12:01 AM, Theo Graham-brown  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## flash[:notice] = “Invalid ID” else
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

There’s a sidebar in AWDWR that discusses that. The gist is that if  
you do User.find(1), then you expect that record to exist. If you do  
User.find\_by\_some\_attribute(“foo”), then it’s more of a scan, and not  
finding a result isn’t exceptional.

You can use User.find\_by\_id(1) if you want those same no-error  
semantics. I think it’s best to use whichever expresses your intent  
better, rather than simply trying for uniformity.

Pat

---

<div class="post-metadata">

**Author:** ![theogb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bb73d2/32.png) [@theogb](https://www.ruby-forum.com/u/theogb)\
**Post date:** [January 11, 2008, 9:22am UTC](https://www.ruby-forum.com/t/dynamic-finders-dont-give-exceptions-or-i-cant-get-any/125700/3 "2008-01-11T09:22:59Z")

</div>

Pat M. wrote:

> On Jan 11, 2008 12:01 AM, Theo Graham-brown  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ## flash[:notice] = “Invalid ID” else
> > 
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> > 
> > >
> 
> There’s a sidebar in AWDWR that discusses that. The gist is that if  
> you do User.find(1), then you expect that record to exist. If you do  
> User.find\_by\_some\_attribute(“foo”), then it’s more of a scan, and not  
> finding a result isn’t exceptional.
> 
> You can use User.find\_by\_id(1) if you want those same no-error  
> semantics. I think it’s best to use whichever expresses your intent  
> better, rather than simply trying for uniformity.
> 
> Pat

Okay, that makes sense. I’ll leave it as it is, then. I worry that I am  
a very poor Rails programmer, you see.

With PHP/C#/C++/C/Java stuff it’s so well documentated that you can  
quickly see when you’re writing in a clear and correct style, but I just  
don’t know enough about Rails to know if I’m making a mess of things.

Cheers  
Theo

---

<div class="post-metadata">

**Author:** ![theogb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bb73d2/32.png) [@theogb](https://www.ruby-forum.com/u/theogb)\
**Post date:** [January 11, 2008, 9:37am UTC](https://www.ruby-forum.com/t/dynamic-finders-dont-give-exceptions-or-i-cant-get-any/125700/4 "2008-01-11T09:37:59Z")

</div>

On Jan 11, 2008 12:22 AM, Theo Graham-brown  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > 
> > 
> > Pat
> 
> Okay, that makes sense. I’ll leave it as it is, then. I worry that I am  
> a very poor Rails programmer, you see.
> 
> With PHP/C#/C++/C/Java stuff it’s so well documentated that you can  
> quickly see when you’re writing in a clear and correct style, but I just  
> don’t know enough about Rails to know if I’m making a mess of things.

Pick up a copy of Obie’s book, The Rails Way. It’s all about  
documenting the Rails way of doing things.

Pat
