Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

See all tables, all records from console?

How can I display the current record counts from all my database tables in one command using the console? Thanks!

like image 908
sscirrus Avatar asked Dec 31 '10 20:12

sscirrus


2 Answers

This will do it if you've 'touched' all your classes, but only for actual models:

ActiveRecord::Base.subclasses.map { |c| "#{c.name} => #{c.count}" }

If you really want all tables, including join tables that don't map to models:

ActiveRecord::Base.connection.tables.map { |t| "#{t} => " + ActiveRecord::Base.connection.execute("select count(*) from #{t}").fetch_row.first}
like image 110
Brian Deterling Avatar answered Oct 09 '22 03:10

Brian Deterling


ActiveRecord::Base.connection.tables

this will return an array of the tables you have, if you find it useful.

like image 43
Sherwyn Goh Avatar answered Oct 09 '22 04:10

Sherwyn Goh