10 Tips and Shortcuts for Rails Console

Tram Ho


With the Rails console, you can interact with the Rails application from the command line without using a web browser. It is a powerful IRB sell loaded with the Rails environment.

You can also use the Rails console to query databases, test or debug rails applications without using browser interaction.

Many developers at the beginner level are not aware of this fact. You can test the command with the backend of the application. These methods here may make it easier for you.

You no longer have to wait in front of the browser to check your output.

Starting Console

Now, we run the rails console with the following command:

Or brevity

Changing Environment

By default when you run the rails console, it will be enabled in development mode.

If you rent to open the Rails console in a specific environment, you can use the -e option with the environment name.

For example, we want to open the Rails console in production mode:

Clearing Console

Sometimes we do not like working at the bottom of the window or when there is large output data on the workspace. The Clear command will not work in the Rails console, it will return the following:

If we want to clear the rails console, we can use ctrl + l in a Linux environment or the + k command to clear on mac.

Reloading Console

In the development environment, the application code will automatically reload for every new request. Sometimes when you change or add a new directory, you need to reload the browser to see the changes in the rails application.

The Rails console is running by default in a development environment, but it doesn’t reload new changes. At the beginning of the rails console, all files are loaded and cached until the end of the rails console. If you need a new change in the rails console, you can exit the rails console and start over. But it is not a good idea when we often change the code. Instead of this, we can use the following command to refresh new changes:


Rails console has default built-in autocomplete functionality. When you start entering the class name and press TAB, it will automatically finish it or it will display a list of how options are available. It will only work with default Ruby and Rails integration classes.

We can also use autocomplate for the method name of any class or object.

Assuming we want autocomplete for the model and controllder available in the / app directory, this can’t be done the first time. If we used the rails console, we could.

For example, we have the Product model. We have used it in the rails console and now we want to use it to run the query. Now we can type Pro, it will return the available options.

Assuming you type ‘Prod, it will autocomplete the Product model.

Suppose you want to autocomplete the methods defined by the programmer in the model, we take it from the Product object.

Searching Command History

Assuming we want to return queries that have been used or to modify queries and run them, the rails console provides two options.

  1. We use the up and down arrows, you should remember the commands used earlier. Using the up arrow, we can see the code that has been used before and use the down arrow to go back.
  2. Assuming you are using a lot of queries in the rails console, using the arrows to find the query will take longer. We can use the Unix bash shell search command here. By using Ctrl + r, we can search the previously used query in the back and return the first query that matches our search string.

Last Expression

Sometimes, once you have typed the query and hit enter, it returns the result. Based on this result, you can perform a number of operations. Say, you forgot to mount a variable, if this situation arises, simply press the up arrow and assign it to the variable and enter.

The Rails console provides great functionality for this. It stores the last variable in the variable ‘_’ (underlined).

Now pressing enter will return the result, we can assign those results in a number of variables. The _ value will constantly change based on the last expression we run.

Now we check the variable _. It has a value of 12 because it only stores the last expression value.

Disable CSRF Token

For example, we want to test the create product method via the console, where we can use the following line to call the create method for the product:

It will return the response:

Using the following command we can disable the authentication of the CSRF token.

Now we can call the create method to create a product, which will work as expected.


Sanbox is a great option for interacting with rails applications, especially in development environments. It will restore or revert all changes we made in the database when we exit the rails console.

For example, we will test some code in the rails console without changing data, we can call the rails console with the -sandbox option.

Next we delete 1 product to test the -sandbox option.

For the above example, we deleted a product, when we exited the rails console, all database operations were restored. To start a new operation and test whether the database operations have actually been restored or not.

If we really need to change the database, we delete the -sandbox option.

Source Location

While working with the rails console, the source_location method we can see that the method is defined and executes the details of the method. For example, our Product model has a discount_amount instance method, we can get the location of the discount_amount method as follows:

We can call soure_location directly. First, we call instance_method on the Product model by passing the method name as an argument, which will return the object, which represents the discount_amount method. From this object, we can call the soure position of the method.

The soure position returns the output as an array of two values. The first value represents the location of the file and the second value represents the number of lines where the method is defined.

If you want to know all the available helper methods, use the following command in the rails console, which will return a list of helper methods.

Listing tables

Assuming you want to know how many tables we have used in our database, you can use the following command to list all table names:


So here are 10 tips and abbreviations for the raisl console, hope you can apply it when you need it: v.

Reference: https://www.agiratech.com/rails-console-shortcuts-to-boost-productivity-ruby-on-rails-guide/

Share the news now

Source : Viblo