Forum: Ruby on Rails catch "find" exception

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Liang G. (Guest)
on 2006-04-29 10:41
obviously, follow code is not working

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

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

Thanks.
Liang G. (Guest)
on 2006-04-30 05:06
Any takers?

liang gao wrote:
> obviously, follow code is not working
>
> <p>
> <% if !@group.users.find(session[:user].id) %>
> <%= link_to "Join This Group", :action => "join", :id => @group %>
> <% end %>
> </p>
>
> "find" always throw an exception. I want to display the link of "find"
> fails, what is the right way to do this?
>
> Thanks.
Danny B. (Guest)
on 2006-04-30 20:19
liang gao wrote:
> obviously, follow code is not working
>
> <p>
> <% if !@group.users.find(session[:user].id) %>
> <%= link_to "Join This Group", :action => "join", :id => @group %>
> <% end %>
> </p>
>
> "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
Charlie B. (Guest)
on 2006-04-30 22:22
liang gao wrote:
> obviously, follow code is not working
>
> <p>
> <% if !@group.users.find(session[:user].id) %>
> <%= link_to "Join This Group", :action => "join", :id => @group %>
> <% end %>
> </p>
>
> "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
Guest (Guest)
on 2006-05-03 05:05
Thanks, Danny.

That solved my problem greatly.

Liang

Daniel B. wrote:
> liang gao wrote:
>> obviously, follow code is not working
>>
>> <p>
>> <% if !@group.users.find(session[:user].id) %>
>> <%= link_to "Join This Group", :action => "join", :id => @group %>
>> <% end %>
>> </p>
>>
>> "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
Mark Reginald J. (Guest)
on 2006-05-03 21:08
(Received via mailing list)
liang gao wrote:
> obviously, follow code is not working
>
> <p>
> <% if !@group.users.find(session[:user].id) %>
> <%= link_to "Join This Group", :action => "join", :id => @group %>
> <% end %>
> </p>
>
> "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.
Mike G. (Guest)
on 2006-05-03 21:10
(Received via mailing list)
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
Eugenol (Guest)
on 2006-05-19 18:52
(Received via mailing list)
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
This topic is locked and can not be replied to.