Andrew
Andrew

Reputation: 238707

How to download data from a Ruby on Rails console running on Heroku?

I have an app running on Heroku that has some data in a database that I need to download into a format that will be easy to work with locally (json, csv, serialized Ruby object, etc). I know how to connect to the Rails console remotely:

heroku run console

Then I can fetch my model data from the database:

records_json = Record.all.to_json

Now that I have the data, how do I download it?

Upvotes: 0

Views: 1037

Answers (1)

scilence
scilence

Reputation: 1115

If it's not a ton of data, you should just be able to copy and paste the output to a file. Run from the console

puts Record.all.to_json

If it is a lot of data, you can run

heroku run:detached rails runner "puts Record.all.to_json"

Per the documentation, the output will go to your logs so you could copy & paste from there. Read more here: https://devcenter.heroku.com/articles/heroku-cli-commands#heroku-run-detached

If you want to be able to download a json file at any time of just this model, you could add a json response to any controller method, like this

respond_to do |format|
  format.html
  format.json { render json: Record.all }
end

Then just navigate to /the_path_you_chose.json

Upvotes: 3

Related Questions