How to export a Ruby Array from my Heroku console into CSV?

后端 未结 10 908
悲&欢浪女
悲&欢浪女 2021-02-02 11:17

I am looking to export an array from my heroku console into a local CSV file.

In my current situation, I have a daily rake task which looks for tweets talking about my a

相关标签:
10条回答
  • 2021-02-02 11:31

    I tried with the following steps with the new heroku console.I was able to get the logs.

    1. type command script output.txt (this creates a file named output.txt for saving terminal logs)
    2. open heroku console and execute commands.

    3. when you are done type ctrl + d.

    Terminal logs are saved in output.txt file.

    0 讨论(0)
  • 2021-02-02 11:32

    You can run Ruby on your Heroku instance:

    echo 'p User.first' | heroku run --no-tty 'ruby -W0 -r ./config/environment' > output.txt

    This will print the first user to the output.txt file.

    Also you can run a local script on Heroku by piping it to the stdin.

    cat my_script.rb | heroku run --no-tty 'ruby -W0 -r ./config/environment' > output.txt

    Note that it's possible that you'll get some unwanted text (like warnings) that will appear at the beginning of the output.txt file. You will have to trim it manually or by using a trimming command such as:

    tail -n +4 -f that will not print the first 4 lines.

    Here is the full example:

    cat my_script.rb | heroku run --no-tty 'ruby -W0 -r ./config/environment' | tail -n +4 -f > output.txt

    0 讨论(0)
  • 2021-02-02 11:33

    I tried using Tee as suggested and also got stuck at

    Running `console` attached to terminal... up, run.4165
    

    I ended up running an SSH shell to localhost and then piped it through tee.

    $ ssh localhost | tee output.txt
    $ heroku run console
    

    Might not be the best solution, but it worked for me.

    0 讨论(0)
  • 2021-02-02 11:38

    I'd use Taps to export the db to your local machine, then operate on it there: https://devcenter.heroku.com/articles/taps

    0 讨论(0)
提交回复
热议问题