Video

Want to see the full-length video right now for free?

Notes

The Rails console is a Rails developer’s best friend. On this week’s episode we take a look at some little-known tips and tricks for increasing your efficiency in the console.

Pry

By default, when you run rails console you will enter an irb session. irb is likely familiar to you and may even be the first environment you ever ran Ruby code in. However, Pry is a more powerful alternative that we tend to use in our Rails projects. To use Pry when you run rails console, add pry-rails to your app’s Gemfile. As we move through this episode, you’ll see examples of where Pry shines.

Entering and Exiting the Console

By default, when you run rails console you’ll start a session in the current RAILS_ENV. Locally, this will be development, but in your deployed environments this may be production or staging. Sometimes it’s useful to launch the console in a different environment. For example, iyou may want to use the test environment if you want to interact with gems that are only available in that environment. The rails console command takes an additional argument for the environment. To start in the test environment run rails console test.

It’s also possible to start the console in sandbox mode, regardless of the environment you use. In sandbox mode, the entire console session will be wrapped in an ActiveRecord transaction, which is rolled back when you exit the console. This is useful if you want to create a bunch of test data in the console but not mess up your development data or want to avoid accidentally modifying production data when running a console in production. To start in sandbox mode, pass the --sandbox flag, like so: rails console --sandbox.

Exiting the console can be done by running exit from within it. We see many people do this regularly. One interesting thing to note is that the console, and many other shells such as psql, bash, zsh, etc, can be exited with CTRL-D. I like to remember this as standing for disconnect, but in reality it’s the Unix end-of-file signal.

There are a number of keyboard shortcuts that work in the Rails console, and conveniently in any other readline-based tool such as your shell, psql, and even many GUI applications.

  • Press the up arrow to cycle through your previous command history.
  • Search backwards through your command history with CTRL-R. As you type, you’ll see matching commands from your history. Repeatedly pressing CTRL-R will find older matches.
  • You can use emacs key bindings to move around within a line. In particular, CTRL-A (beginning of line), CTRL-E (end of line), ALT-F (forward to end of next word) and ALT-B (back to start of the current or previous word) are handy to know in many contexts.

For more information on readline, see our earlier Weekly Iteration episode

Reloading Code

It’s not uncommon to modify code while you have a Rails console session running. I often see developers exit and relaunch the console to access the freshly-edited code, but this is not necessary. The reload! method available to you in a console session will reload all of your classes off disk. One caveat to be aware of is that individual instances of classes will need to be reinitialized to access new code. For example:

> outcome = Outcome.first
> outcome.to_s
> # edit the definition of to_s outside of the console
> outcome = Outcome.find(outcome.id)
> outcome.to_s # new definition of to_s accessed

Exploring code with Pry

Pry has many methods available to you to aid in exploring and debugging code. For example:

  • View a method’s source with show-source. For example, show-source outcome.to_s will show the definition of Outcome#to_s. If that method calls super, you can look at the superclass definition with show-source outcome.to_s -s, chaining additional ss to navigate up the inheritance chain.
  • List available methods on an object with ls. For example ls outcome will show you all available methods on instances of Outcome broken down by the class or module they were defined in.
  • You can change the scope of your current console session with the cd method. For example cd outcome will put me in the scope of that particular instance of Outcome. This allows me to directly inspect state, call private methods, etc., without the need for things like instance_variable_get or send.
  • Pry has great documentation available from directly within the console. The help method will show all available commands with a brief synopsis of what they can be used for. Additional help is available by adding --help to any Pry method.

See our earlier Weekly Iteration episode on debugging for more useful Pry debugging tips.

Special Methods

The Rails console has many special methods available to you to for convenience:

  • _ will return you the value of the last successful expression. This is useful in cases where you run a command and then decide you want to save a reference to it. For example, if I ran Outcome.find(1) and decided I needed to save a reference to the resultant instance, I could run outcome = _ to capture it in the outcome variable.
  • The app method provides access to many high level methods in rails applications such as path helpers (app.root_path) and HTTP methods (app.get(app.root_path)).
  • The helper method provides access to view helpers — both those defined by your application and those provided by rails, such as link_to.