Testing a query result

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!

On Feb 24, 11:28 am, Mahmou M’hiri [email protected] wrote:

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.

that’s true!

The easiest way to do this is using the exists? function.
Example:

if Book.exists?(params[:id])
@BookDetails = Book.find(params[:id])
else
#No results
end

Phrogz wrote:

On Feb 24, 11:28 am, Mahmou M’hiri [email protected] wrote:

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