Record not found

hi all,

I am searching the database to show some clients with the following
code:

@client = Client.find(params[:id],
:conditions => [‘user_id = ?’,session[:user_id]])

if the combination is ok - i see the record on my page
if not - RoR shoot an error:

Couldn’t find Client with ID=16 AND (user_id = 5)

how can I catch that error?

thanks for any tip
andreas

Andreas S. wrote:

hi all,

I am searching the database to show some clients with the following
code:

@client = Client.find(params[:id],
:conditions => [‘user_id = ?’,session[:user_id]])

if the combination is ok - i see the record on my page
if not - RoR shoot an error:

Couldn’t find Client with ID=16 AND (user_id = 5)

how can I catch that error?

thanks for any tip
andreas

got it:
@client = Client.find(:first, :conditions => [‘id = ? and user_id =
?’, params[:id], session[:user_id]])

if u want to catch an error, use “rescue”
usually in a method/action inside a controller u add like these (if you
want to catch/raise errors on record not found)

def your_method
your_code…

rescue ActiveRecord:RecordNotFound
insert_your_code_here
end

Andreas S. wrote:

Andreas S. wrote:

hi all,

I am searching the database to show some clients with the following
code:

@client = Client.find(params[:id],
:conditions => [‘user_id = ?’,session[:user_id]])

if the combination is ok - i see the record on my page
if not - RoR shoot an error:

Couldn’t find Client with ID=16 AND (user_id = 5)

how can I catch that error?

thanks for any tip
andreas

got it:
@client = Client.find(:first, :conditions => [‘id = ? and user_id =
?’, params[:id], session[:user_id]])

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs