What's the easiest method to test to have an empty database in rails? I produced one chardata and also the controller, Chardatum. I wish to loop with the database and extract a particular column, however i need a method to test when the database is empty first.

It is dependent on a couple of things.

What type of database are you currently using? By empty, would you mean the tables haven't been produced yet? or simply that data is not placed yet to the tables?

To ascertain if the chardatas table doesn't have rows before proceeding:


Presuming you are using MySQL, you should use the next command to ascertain if tables happen to be produced already inside your database

Chardata.count_by_sql "SELECT COUNT(DISTINCT 'table_name') FROM 'information_schema'.'columns' WHERE 'table_schema' = 'your_db_name'".zero?

If you're fetching some Chardata allready you could do this:


You may also see if the table continues to be produced with CharDatum.table_exists?.