How to check if a table is empty ?

Hi In my web application, a table must have only one record. So when there is no record, user can add a new row to the table, (a add link is shown) and when a record is already available, user can't add a new one, he can just edit or destroy it( after destroying the current one, no more records exist, so he can add a new one). But I don't know how to check a table for emptiness. The other problem is when there is one record in table, how can I have a link to its show action(made by scaffolding), while i don't know the id. Because if the user destroy the record, its id is not 1 any more.

Hello,

For the first part of your problem, you can check emptiness for your table Example with Example.find(:all).empty? => boolean

For the second part, @object = Example.find(:first) your_path = example_path(@object)

Of course you can probably do it with a better looking method, but basically that’s it.

Cyril

Nice approach, thanks I did it and it works now.

Another approach to checking for emptyness in case you need it:

Table.count.zero?

Typically I use something like 'unless Model.first', but are any of these techniques particularly lighter/faster on the db than others?

-eric

> > But the variable is unnecessary, and in fact, a more concise way of > > saying the same thing would be

> > if Model.find(:all).empty?

> Agreed. I was just showing that there are more ways of doing things. I > use your approach...

> .empty? > .nil?

> .. two things I use heavily in my projects...

Typically I use something like 'unless Model.first', but are any of these techniques particularly lighter/faster on the db than others?

It won't make much difference if you expect your table to genuinely only contain either 0 rows or a very small number of rows. If it might contain 10s or hundreds or rows it will be way faster to check Model.first than it would be to check Model.all.empty? (since you'd be loading instantiating all hundred objects rather than just 1)

Fred

But the variable is unnecessary, and in fact, a more concise way of

saying the same thing would be

if Model.find(:all).empty?

Agreed. I was just showing that there are more ways of doing things. I

use your approach…

.empty?

.nil?

… two things I use heavily in my projects…

Typically I use something like ‘unless Model.first’, but are any of

these techniques particularly lighter/faster on the db than others?

It won’t make much difference if you expect your table to genuinely

only contain either 0 rows or a very small number of rows. If it might

contain 10s or hundreds or rows it will be way faster to check

Model.first than it would be to check Model.all.empty? (since you’d be

loading instantiating all hundred objects rather than just 1)

Fred

I agree with Fred’s approach because if you’re testing for emptiness, you

should do the least amount of work to determine it. For example,

Model.first => will create 1 or no objects

Model.all => will create table size of N or no objects

Good luck,

-Conrad

Conrad Taylor wrote: [...]

I agree with Fred's approach because if you're testing for emptiness, you should do the least amount of work to determine it.

Excellent point.

For example,

Model.first => will create 1 or no objects Model.all => will create table size of N or no objects

Better still:

Model.count => will create one Numeric and that's all!

Good luck,

-Conrad

Best,

Conrad Taylor wrote:

[...]

> I agree with Fred's approach because if you're testing for emptiness, > you > should do the least amount of work to determine it.

Excellent point.

> For example,

> Model.first => will create 1 or no objects > Model.all => will create table size of N or no objects

Better still:

Model.count => will create one Numeric and that's all!

In ruby land yes, this is cheaper but not for the database: with Model.first the database gets just one row and then stops, with Model.count it has to count the whole table (which can be expensive on a large table - usually the db will scan an appropriate index)

Fred

I think this is the classic scenario to use find_or_create_by_XXXX

  # No 'Summer' tag exists   Tag.find_or_create_by_name("Summer") # equal to Tag.create(:name => "Summer")

  # Now the 'Summer' tag does exist   Tag.find_or_create_by_name("Summer") # equal to Tag.find_by_name ("Summer") you can see the documentation here ActiveRecord::Base