0

Is it possible to spawn rails console from nodejs and then execute the script in rails console ?

const { spawn } = require('child_process');

const child_process = spawn('rails c');

child_process.stdout.on('data', function (data) {
  console.log('stdout', data.toString());
});

child_process.stderr.on('data', function (data) {
  console.log('stderr', data);
});

child_process.on('close', function (code) {
  console.log('close', code);
});

Upon opening the console, i wanted to execute this script in the rails console


user_id = 1624522827
Sharding.get_shard(user_id) do
  account = UserList::User.find(user_id).make_user
end

How to achieve this in nodejs. My intention is to create a cli that runs script inside rails console.

2 Answers 2

2

I think what you want is rails runner instead of console. Either pass it a line of ruby code or a filename. It will run in the rails environment, not just the ruby irb environment.

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

Comments

0

The ruby console is for humans. I wouldn't try and pipe input into it.

A better idea is to use a rake task and then call that with

bundle exec rake task:subtask

guide for rake tasks

All your code should go in the rake task and then you can call it from nodejs using just the filepath.

Comments

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.