Decaflon

Welcome to Decaflon! Where the geeks hang out: Signup or Login Here
Decaflon is proudly hosted by (mt) Media Temple.  We recommend them for your web hosting needs.
Clips: Popular Clips Upcoming Clips Notes: All Notes

I am doing a query for some data, if the query is empty or returns no data because there is none. How do I show that the query was empty?

Here I am finding Todo’s via params[:id]
@todo = Todo.find(params[:id])

Now if this comes back with some data it will list out what is in the database. But if it returns nothing how do I show this?
if @todo == nil
@todo = "Sorry No Todo's!"
end

I have tried an if statement (above) but it does not work... How do I show the following text? “Sorry No Todo’s” if there is nothing in the database?

Have you tried if @todo.length == 0? I'm not sure if it's completely correct in this situation though.

you can try:

if (todo == "") todo = "Sorry No Todo's!"

if (todo == null) todo = "Sorry No Todo's!"

if (!todo) todo = "Sorry No Todo's!"

if (todo<=0) todo = "Sorry No Todo's!"

Hey, well here is what i ended up with.

if @todos.blank?
flash[:notodo] = 'Sorry there are no todos.'
end

This is working as i needed, is there a better way to do this?

@array.blank? is the best way to check if array is empty.

Please Login To Leave A Comment

Decaflon Sponsors Get in touch if you want in.

 

Decaflon is part of the Chawlk Network of sites.

9 Great Places To Visit, Hang Out, & Meet New People

What's new and interesting at other Chawlk Network sites: