# Catch "find" exception

**URL:** <https://www.ruby-forum.com/t/catch-find-exception/59158>\
**Category:** Rails\
**Created:** [April 29, 2006, 8:41am UTC](https://www.ruby-forum.com/t/catch-find-exception/59158 "2006-04-29T08:41:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![lgao](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lgao](https://www.ruby-forum.com/u/lgao)\
**Post date:** [April 29, 2006, 8:41am UTC](https://www.ruby-forum.com/t/catch-find-exception/59158/1 "2006-04-29T08:41:43Z")

</div>

obviously, follow code is not working

\<% if !@group.users.find(session[:user].id) %\> \<%= link\_to "Join This Group", :action =\> "join", :id =\> @group %\> \<% end %\>

“find” always throw an exception. I want to display the link of “find”  
fails, what is the right way to do this?

Thanks.

---

<div class="post-metadata">

**Author:** ![lgao](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lgao](https://www.ruby-forum.com/u/lgao)\
**Post date:** [April 30, 2006, 3:06am UTC](https://www.ruby-forum.com/t/catch-find-exception/59158/2 "2006-04-30T03:06:58Z")

</div>

Any takers?

liang gao wrote:

> obviously, follow code is not working
> 
> \<% if !@group.users.find(session[:user].id) %\> \<%= link\_to "Join This Group", :action =\> "join", :id =\> @group %\> \<% end %\>
> 
> “find” always throw an exception. I want to display the link of “find”  
> fails, what is the right way to do this?
> 
> Thanks.

---

<div class="post-metadata">

**Author:** ![lgao](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lgao](https://www.ruby-forum.com/u/lgao)\
**Post date:** [April 30, 2006, 6:19pm UTC](https://www.ruby-forum.com/t/catch-find-exception/59158/3 "2006-04-30T18:19:37Z")

</div>

liang gao wrote:

> obviously, follow code is not working
> 
> \<% if !@group.users.find(session[:user].id) %\> \<%= link\_to "Join This Group", :action =\> "join", :id =\> @group %\> \<% end %\>
> 
> “find” always throw an exception. I want to display the link of “find”  
> fails, what is the right way to do this?
> 
> Thanks.

I prefer to use the dynamic finders, because they return nil on failure,  
rather than throw an exception. Vis-

\<% if !@group.users.find\_by\_id(session[:user].id) %\>

- Danny

---

<div class="post-metadata">

**Author:** ![lgao](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lgao](https://www.ruby-forum.com/u/lgao)\
**Post date:** [May 3, 2006, 3:05am UTC](https://www.ruby-forum.com/t/catch-find-exception/59158/4 "2006-05-03T03:05:15Z")

</div>

Thanks, Danny.

That solved my problem greatly.

Liang

Daniel B. wrote:

> liang gao wrote:
> 
> > obviously, follow code is not working
> > 
> > \<% if !@group.users.find(session[:user].id) %\> \<%= link\_to "Join This Group", :action =\> "join", :id =\> @group %\> \<% end %\>
> > 
> > “find” always throw an exception. I want to display the link of “find”  
> > fails, what is the right way to do this?
> > 
> > Thanks.
> 
> I prefer to use the dynamic finders, because they return nil on failure,  
> rather than throw an exception. Vis-
> 
> \<% if !@group.users.find\_by\_id(session[:user].id) %\>
> 
> - Danny

---

<div class="post-metadata">

**Author:** ![lgao](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lgao](https://www.ruby-forum.com/u/lgao)\
**Post date:** [May 3, 2006, 7:08pm UTC](https://www.ruby-forum.com/t/catch-find-exception/59158/5 "2006-05-03T19:08:25Z")

</div>

liang gao wrote:

> obviously, follow code is not working
> 
> \<% if !@group.users.find(session[:user].id) %\> \<%= link\_to "Join This Group", :action =\> "join", :id =\> @group %\> \<% end %\>
> 
> “find” always throw an exception. I want to display the link of “find”  
> fails, what is the right way to do this?

\<% unless @group.users.find\_by\_id(session[:user].id) %\>

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![lgao](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lgao](https://www.ruby-forum.com/u/lgao)\
**Post date:** [May 3, 2006, 7:10pm UTC](https://www.ruby-forum.com/t/catch-find-exception/59158/6 "2006-05-03T19:10:48Z")

</div>

what’s wrong with Mark J.’ code? find\_by… doesn’t throw an  
exception  
when it fails to find a record, it simply returns nil. That’s what you  
want  
and should be using in this case.

Mike

---

<div class="post-metadata">

**Author:** ![lgao](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lgao](https://www.ruby-forum.com/u/lgao)\
**Post date:** [May 19, 2006, 4:52pm UTC](https://www.ruby-forum.com/t/catch-find-exception/59158/7 "2006-05-19T16:52:21Z")

</div>

On 4/30/06, Mike G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> what’s wrong with Mark J.’ code? find\_by… doesn’t throw an exception  
> when it fails to find a record, it simply returns nil.

Too bad it doesn’t throw an exception. Then I prefere writing in my  
controller:

unless @blog = Blog.find\_by\_id\_and\_is\_archived(params[:id], 0)  
raise ActiveRecord::RecordNotFound, “Couldn’t find a non archived  
Blog with ID=#{params[:id]}”  
end

---

<div class="post-metadata">

**Author:** ![lgao](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lgao](https://www.ruby-forum.com/u/lgao)\
**Post date:** [April 30, 2006, 8:22pm UTC](https://www.ruby-forum.com/t/catch-find-exception/59158/8 "2006-04-30T20:22:13Z")

</div>

liang gao wrote:

> obviously, follow code is not working
> 
> \<% if !@group.users.find(session[:user].id) %\> \<%= link\_to "Join This Group", :action =\> "join", :id =\> @group %\> \<% end %\>
> 
> “find” always throw an exception. I want to display the link of “find”  
> fails, what is the right way to do this?
> 
> Thanks.

I personally try to never put a find in a view. It really belongs in  
the controller. There I would have something like this.

## method to view a particular post in full

def view  
@post = Post.find(params[:id])  
rescue  
logger.error(“Attempt to access invalid post #{params[:id]}”)  
end
