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.
5b8e28dfc732a50566c077f212871ff6?d=identicon&s=25 Liang Gao (lgao)
on 2006-04-29 08: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.
5b8e28dfc732a50566c077f212871ff6?d=identicon&s=25 Liang Gao (lgao)
on 2006-04-30 03: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.
16b26104470ae1b64958f3caa836c193?d=identicon&s=25 Danny Burkes (dburkes)
on 2006-04-30 18: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
73606b1d1042de19056f3d59492b652f?d=identicon&s=25 Charlie Bowman (beachguy82)
on 2006-04-30 20: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
5b8e28dfc732a50566c077f212871ff6?d=identicon&s=25 Guest (Guest)
on 2006-05-03 03:05
Thanks, Danny.

That solved my problem greatly.

Liang

Daniel Burkes 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
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2006-05-03 19: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.
0900e6a4828bd989f96427082c6c74ca?d=identicon&s=25 Mike Garey (random52k)
on 2006-05-03 19:10
(Received via mailing list)
what's wrong with Mark James' 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
E6dcb4743b83392a4fc39693f110cc26?d=identicon&s=25 Eugenol (Guest)
on 2006-05-19 16:52
(Received via mailing list)
On 4/30/06, Mike Garey <random52k@gmail.com> wrote:
> what's wrong with Mark James' 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.