Whats a good way to avoid nil lookup errors

9 views
Skip to first unread message

Christian Fazzini

unread,
Sep 5, 2010, 6:55:48 AM9/5/10
to Ruby on Rails: Talk
When navigating to: http://localhost:3000/users/12

Whats a good way of avoiding the error: "Couldn't find User with
ID=12", which happens when i try to display a page of user_id 12 that
doesnt exist in the database.

Instead of showing the error. id like to show a page that says: "This
user does not exist"

Someone suggested using rescue_from. Is this the best solution for
this? Or are there better alternative approaches?

Ugis Ozols

unread,
Sep 5, 2010, 7:28:24 AM9/5/10
to Ruby on Rails: Talk
One of the solutions would be to use redirect_to and flash[:notice]
like:

@user = User.find(1)
unless @user
flash[:notice] = "can't find user with given id"
redirect_to users_path
end

I won't say it's the best but it's still a solution :)

On 5 sept., 13:55, Christian Fazzini <christian.fazz...@gmail.com>
wrote:

Christian Fazzini

unread,
Sep 5, 2010, 8:57:03 AM9/5/10
to Ruby on Rails: Talk
Yes but, it will never reach "unless." The system fails after @user =
User.find(1) since Active Record is trying to find a record that does
not exists

Michael Pavling

unread,
Sep 5, 2010, 9:21:25 AM9/5/10
to rubyonra...@googlegroups.com
@user = User.find(1) if User.exists?(1)

I don't like doing two lookups for one record, but it works

Simon Macneall

unread,
Sep 5, 2010, 9:30:26 AM9/5/10
to rubyonra...@googlegroups.com
What about
@user = User.find(:first, :conditions => {:id => 1})
if (@user)
blah, blah, blah
end

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:

Rob Biedenharn

unread,
Sep 5, 2010, 9:42:35 AM9/5/10
to rubyonra...@googlegroups.com
On Sep 5, 2010, at 9:30 AM, Simon Macneall wrote:
> On Sun, 05 Sep 2010 21:21:25 +0800, Michael Pavling
> <pav...@gmail.com> wrote:
>
>> @user = User.find(1) if User.exists?(1)
>>
>> I don't like doing two lookups for one record, but it works
>
> What about
> @user = User.find(:first, :conditions => {:id => 1})
> if (@user)
> blah, blah, blah
> end
>
> It's longer to type, but returns nil if the user doesn't exist.

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/

Christian Fazzini

unread,
Sep 5, 2010, 10:38:37 AM9/5/10
to Ruby on Rails: Talk
So this would be the convention right? not by using rescue_from. Is
this right?

Bill Walton

unread,
Sep 5, 2010, 11:00:22 AM9/5/10
to rubyonra...@googlegroups.com
Hi Christian

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

Paul Philippov

unread,
Sep 5, 2010, 7:27:24 AM9/5/10
to Ruby on Rails: Talk
def show
@user = User.find(params[:id])
rescue ActiveRecord::RecordNotFound
redirect_to users_path, :alert => "This user does not exists"
end

Christian Fazzini

unread,
Sep 5, 2010, 12:31:44 PM9/5/10
to Ruby on Rails: Talk
Hi Paul. but wouldnt that redirect to users_path on any activerecord
not found event?

Ugis Ozols

unread,
Sep 5, 2010, 2:04:17 PM9/5/10
to Ruby on Rails: Talk
Good call on that find_by_id. Noted to myself :)

Paul Philippov

unread,
Sep 6, 2010, 3:58:51 AM9/6/10
to Ruby on Rails: Talk
nope, only within the action.

On Sep 5, 11:31 pm, Christian Fazzini <christian.fazz...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages