4

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!

3
  • You might consider making a rake task for this. It's a little more idiomatic. Commented Oct 28, 2015 at 6: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. Commented Oct 28, 2015 at 6:46
  • 1
    Possible duplicate of Pass ruby script file to rails console Commented Oct 28, 2015 at 6:47

1 Answer 1

5

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?

Sign up to request clarification or add additional context in comments.

2 Comments

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!
My use case is to run essentially one-time-use commands, and the first code line is working well for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.