Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 5 How to get a count of all the rows in each table

I had a old script that worked for me on rails 4

ActiveRecord::Base.connection.tables.map { |t| "#{t} => " + ActiveRecord::Base.connection.execute("select count(*) from #{t}").first['count'] }

but this is not returning anything on a rails 5 project :(

like image 936
Jose Perez Avatar asked Feb 08 '17 18:02

Jose Perez


1 Answers

This should work:

  ActiveRecord::Base.connection.tables.map { |t| {t=>  ActiveRecord::Base.connection.execute("select count(*) from #{t}")[0]} }
like image 97
Joel Blum Avatar answered Oct 12 '22 13:10

Joel Blum