Catch "find" exception


#1

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.


#2

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.


#3

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

#4

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

#5

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.


#6

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


#7

On 4/30/06, Mike G. 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


#8

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