3

I am currently trying to figure out if it is possible to run multiple commends within one line on a rails console through Unix shell or within a ruby script.

For Example:

exec('echo p = Product.first;b = Billing.first|rails c')

When I tried something similar to the example, it would always try to execute the two commands before launching console.

Hope this make sense and appreciate any help I can get.

Thanks in advance!

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
jhwang
  • 67
  • 1
  • 8
  • You might consider making a rake task for this. It's a little more idiomatic. – Brad Werth Oct 28 '15 at 06:45
  • Answer to your literal question: more quotes. But it's really a strange thing to do, executing rails console from shell from ruby, almost certainly an XY problem. – Amadan Oct 28 '15 at 06:46
  • 1
    Possible duplicate of [Pass ruby script file to rails console](http://stackoverflow.com/questions/10313181/pass-ruby-script-file-to-rails-console) – Brad Werth Oct 28 '15 at 06:47

1 Answers1

3

Yes you can do this

From a unix shell prompt:

echo 'p = Product.first; b = Billing.first' | rails c

From the rails console itself, or a ruby script I guess:

exec(%Q{echo 'p = Product.first; b = Billing.first' | rails c})

It's probably worth asking why you want to do this though. Could you use a rake task?

Devin Howard
  • 675
  • 1
  • 6
  • 17
  • Thanks for the quick reply! I haven't tried writing my own rake task before but it is something I should explore. I was just kind of messing around do to my curiosity. Thanks again everyone for all the help! – jhwang Oct 28 '15 at 07:02
  • My use case is to run essentially one-time-use commands, and the first code line is working well for me. – Pysis May 22 '17 at 15:22