Catch "find" exception

obviously, follow code is not working

<% if [email protected](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.

Any takers?

liang gao wrote:

obviously, follow code is not working

<% if [email protected](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.

liang gao wrote:

obviously, follow code is not working

<% if [email protected](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

Thanks, Danny.

That solved my problem greatly.

Liang

Daniel B. wrote:

liang gao wrote:

obviously, follow code is not working

<% if [email protected](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

liang gao wrote:

obviously, follow code is not working

<% if [email protected](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.

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

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

liang gao wrote:

obviously, follow code is not working

<% if [email protected](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