[Ruby on Rails (RoR)] Best practices about Rails Console
Best practices about Rails Console
The rails console
command lets you interact with your Rails application from the command line. On the underside, rails console uses IRB deault, so if you’ve ever used it, you’ll be right at home. This is useful for testing out quick ideas with code and changing data server-side without touching the website.
You can also use the alias “c” to invoke the console: rails c
.
How to use
Starting Console
Now, we can start rails console using the following command,
1 | $ rails console |
Changing Environment
By default when you run the rails console it fires up in development mode.
1 | $ rails c |
If you want to open Rails console in a specific environment, you can use -e
option with the environment name.
For example, we want to open Rails console in production mode, Look below.
1 | # production mode |
Loading production environment.
Sandbox
Sandbox is a very good option for interacting rails application, especially in a production environment. It will roll back or revert all the changes we made in the database once we exit the console session.
If you wish to test out some code without changing any data, you can do that by invoking rails console --sandbox.
1 | $ rails c --sandbox |
For the above example, I have deleted a product, when you exit from console session all the database transactions are rollbacked. To start a new session and check it if the database transactions are actually rollbacked or not.
1 | $ rails c |
Clearing Console
Sometimes I loathe working at the bottom of the window or when having a huge output data above the space I am working. If you want to clear rails console you can use ctrl + l
in a Linux environment or command + k
to clear in mac.
Reloading Console
Rails console is running default in the development environment, but it does not reload the new change. At the time of starting rails console, all the files are loaded and stored in a cache up to the end of Rails console. If you need new changes in rails console you can exit from the console and starting a new console session. But it is not a good idea when we frequently changing the code. Instead of this, we can use the following command to refresh new changes.
1 | reload! |
Tab Autocomplete
Rails console has default built-in autocomplete functionality. When start typing class name and press TAB
once or two, it will autocomplete it or it will display the list of available options. It will work only for default Ruby and Rails built-in class.
1 | # Ra with TAB |
Searching Command History
Suppose we want to return the already used query or to modify the query and run, the console provides two options.
-
We can use the up and down arrow, you should recall the previously used commands. Using up arrow ↑ we can to go previously used code by using down arrow ↓ to go back.
-
suppose you have used a lot of queries in console, using up arrow to finding the query is taking more time. We can use Unix bash shell search command here. By using
Ctrl + r
we can search the previously used query in backward and return the first query matched on our search string. -
in order to enable commands history on rails console or irb you can just add the following to your
~/.irbrc
file:
Create, or edit your ~/.irbrc
file to include:
1 | require 'irb/ext/save-history' |
So if you close your rails console or irb the history is persisted. So next time just use the arrows UP ↑ /DOWN ↓ to get your history back.
Last Expression
Sometimes, having typed the query and press enter, it will return the result. Based on this result you can perform some operation. Let’s say, you forgot to assign the result in a variable. If this situation arises, just press the Up arrow and assign to variable and return it.
Rails console provides amazing functionality for this. It stores the last expression in _
(underscore) variable.
1 | 'Ruby On Rails' |
Extracting frequent statements into class method
When you often need to execute multiple statements in the Rails Console to complete tasks, it is recommended to extract these statements into service or model class methods.
Then, call service or model class methods in the Rails Console.