If you need to download some data from Heroku as i did, here is a helpful command to do it.
First, i will create a Ruby file to execute what i need in the Rails console, for example:
report = CSV.generate do |csv| csv << ["name", "email"] users = User.where( :created_at => Date.parse("01-11-2020")..Date.parse("30-11-2020") ) users.each do |user| csv << [user.name, user.email] end end STDOUT.puts report
In this case, i'm selecting all users created from November 1st until the 30th of November and exporting a csv file with all users names and emails.
In order to run this script in a Heroku Rails console and download this CSV file, we are going to use the Rails runner command.
cat users.rb | heroku run --no-tty --app=heroku-app-name -- bin/rails runner - > users.csv
If you have NewRelic installed in your Rails application on Heroku, probably you will get some limes from it inside your CSV file. To fix that, add this grep option in our command:
cat users.rb | heroku run --no-tty --app=heroku-app-name -- bin/rails runner - | grep -v "\*\* \[NewRelic\]" > users.csv