Anyone who works with Rails has heard of and used the runner command with a single-line code argument that executes in the application context.
While working on my application the other day, I needed to list all tables with their row counts.
The code for listing the number of table rows on a single line looks rather ugly and unmaintainable.
If you read the runner help (bin/rails runner -h), you’ll see that runner can also read code from stdin.
To pass multi-line code to runner, you can use a heredoc.
To me, this looks absolutely stunning!
$ bin/rails r - << EOM > ../tables_`date --iso-8601=seconds`.txt
ApplicationRecord.connection_pool.with_connection do |c|
c.tables.sort.each do |table|
c.execute(<<~SQL).to_a.first.first.then do |count|
SELECT COUNT(*) FROM #{c.quote_table_name(table)};
SQL
puts "#{table}: #{count}"
end
end
end
EOM