I don't like doing two lookups for one record, but it works
It's longer to type, but returns nil if the user doesn't exist.
On Sun, 05 Sep 2010 21:21:25 +0800, Michael Pavling <pav...@gmail.com>
wrote:
Ugis almost has it. Try this
@user = User.find_by_id(1)
unless @user
flash[:notice] = "can't find user with given id"
redirect_to users_path
end
ActiveRecord::Base#find_by_id is a dynamic finder that will work just
like the line Simon has above--nil if there is no record found.
-Rob
Rob Biedenharn
R...@AgileConsultingLLC.com http://AgileConsultingLLC.com/
r...@GaslightSoftware.com http://GaslightSoftware.com/
On Sun, Sep 5, 2010 at 9:38 AM, Christian Fazzini
<christia...@gmail.com> wrote:
> So this would be the convention right? not by using rescue_from. Is
> this right?
Yes. When possible, avoid begin-rescue by using Ruby / Rails to
return a value you can handle within the normal course of your
application logic. It makes for more readable code.
Best regards,
Bill