Testing a query result

M

Mahmou M'hiri

Hello every body,

It's a very basic question, i want just to test if this query did return
any result.

@BookDetails = Book.find(params[:id])


I tried "nil", but it seems that even if there isn't any book with that
id, @BookDetails is != nil.

Help please!
 
P

Phrogz

It's a very basic question, i want just to test if this query did return
any result.

@BookDetails = Book.find(params[:id])

You will probably get better results asking this question on the Ruby
on Rails discussion forum. You have instead asked it on the ruby-talk
mailing list.
 
S

Stefan Mahlitz

Phrogz said:
It's a very basic question, i want just to test if this query did return
any result.

@BookDetails = Book.find(params[:id])

You will probably get better results asking this question on the Ruby
on Rails discussion forum. You have instead asked it on the ruby-talk
mailing list.

I actually think that this is an ActiveRecord question. Does this
qualify for a redirect to rails (even if params[:id] is a good hint)?

I have to admit that I do not know whether rails does change this behaviour.

Do we cover ActiveRecord questions on ruby-talk?


To answer the original question:

ActiveRecord will raise an ActiveRecord::RecordNotFound exception which
you can detect/catch via

begin
@BookDetails = Book.find(params[:id])
rescue ActiveRecord::RecordNotFound
# the query did not return a result
end

Stefan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,234
Messages
2,571,180
Members
47,813
Latest member
RustyGary3

Latest Threads

Top