Run Rails Commands Outside of Console

Run Rails commands outside of console

There are two main ways to run commands outside console:

  1. Rake task which depends on :environment

  2. rails runner (previously script/runner), eg:

    $ rails runner "query"

Both are pretty well documented on the rails guide: https://guides.rubyonrails.org/command_line.html#bin-rails-runner

Both of these methods will still take the same time as a console to fire up, but they are useful for non-interactive tasks.

How to run some default commands after firing the Rails console?

Rails' console is IRB. IRB supports an .irbrc file, which contains initialization information and settings.

Read "https://stackoverflow.com/questions/123494/whats-your-favourite-irb-trick" and "My .irbrc for console/irb" for ideas.

How to run rails commands in production with capistrano 3 and rvm installed

Solved with the help of this Upwork freelancer.

The solution steps were:

  • remove the binstubs locally
  • set :bundle_binstubs, nil in config/deploy.rb
  • remove the bin directory from the :linked_dirs list (adding also /bin in .gitignore)
  • push the changes and run cap production deploy
  • recreate the binstubs with rake rails:update:bin
  • comment out the set :bundle_binstubs, nil line
  • add the bin directory in :linked_dirs again
  • modify the config/deploy.rb file like this:
namespace :deploy do
task :regenerate_bins do
on roles(:web) do
within release_path do
execute :bundle, 'exec rake rails:update:bin'
end
end
end
...

...
after :finishing, :regenerate_bins
...
  • uncomment set :bundle_binstubs, nil and remove bin from :linked_dirs once more
  • push changes and deploy

After this, the binstubs are found in the current/bin directory instead of the shared/bin one (in Rails 4 and 5)

Pass ruby script file to rails console

In the meantime, this solution has been supported.

rails r PATH_TO_RUBY_FILE

Much simpler now.

Run command on Rails Console startup

You can write ruby code in irb config file. Edit the .irbrc file and add your ruby code used to determine whether you connected to local or remote

if defined? Rails
//ruby code to find you connected to local or remote
puts "USING REMOTE DATABASE...."
end

rails console command not working

I have figured it out.

I just remove bin directory and run the command to regenerate bin again.

Running below commands solve my problem.

rm -r bin/*

bundle exec rake rails:update:bin


Related Topics



Leave a reply



Submit