Want to see the full-length video right now for free?Sign In with GitHub for Free Access
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.
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.
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
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
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.
CTRL-R. As you type, you’ll see matching commands from your history. Repeatedly pressing
CTRL-Rwill find older matches.
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
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
Pry has many methods available to you to aid in exploring and debugging code. For example:
show-source. For example,
show-source outcome.to_swill 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.
ls. For example
ls outcomewill show you all available methods on instances of
Outcomebroken down by the class or module they were defined in.
cdmethod. For example
cd outcomewill 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
helpmethod will show all available commands with a brief synopsis of what they can be used for. Additional help is available by adding
--helpto any Pry method.
See our earlier Weekly Iteration episode on debugging for more useful Pry debugging tips.
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
appmethod provides access to many high level methods in rails applications such as path helpers (
app.root_path) and HTTP methods (
helpermethod provides access to view helpers — both those defined by your application and those provided by rails, such as