Reputation: 106
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 :(
Upvotes: 8
Views: 2969
Reputation: 1332
ActiveRecord::Base.connection.tables.inject({}) { |h,t| h[t] = ActiveRecord::Base.connection.execute("select count(*) from #{t}")[0].values.first; h }
will produce nice output like:
{
"users" => 12,
"teams" => 5
}
Upvotes: 1
Reputation: 7888
This should work:
ActiveRecord::Base.connection.tables.map { |t| {t=> ActiveRecord::Base.connection.execute("select count(*) from #{t}")[0]} }
Upvotes: 17