Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Rails commands outside of console

With my large application, the Rails console takes a while to load up. Is there a way to single commands more easily?

I'd also like to be able to automate stuff, and echo "query" | rails console isn't a great way to do things.

Thoughts?

EDIT: What about a long-running process that I can ping queries to whenever I have need?

like image 670
tekknolagi Avatar asked Jun 19 '12 20:06

tekknolagi


People also ask

How do I run a command in Rails?

Go to your browser and open http://localhost:3000, you will see a basic Rails app running. You can also use the alias "s" to start the server: bin/rails s . The server can be run on a different port using the -p option. The default development environment can be changed using -e .

How do you escape the console in Rails?

To exit the console, type: quit .

How do I close the console in Ruby?

Type quit() and hit Enter to exit ruby.


1 Answers

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

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

like image 107
Tim Peters Avatar answered Sep 18 '22 01:09

Tim Peters