Introduction to the Rails Console
Learn the basics of the Rails console
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

Now that you know about scaffolds and how the file system works, it's time to know how to access data without using the browser. There is an built in tool for this, and it is called the Rails Console.

Technically, you can do everything you want through the file system and browser, but doing it on the console increases efficiency and saves you time.

For example, if you want to run a query in the database and see the results in a browser, you need to update the controller and view files and check if all the columns are captured. Then, you have to refresh the browser to see if it populated the query. If you want to know the number of records it returned, then you have to manually count them on your browser, and all this is too time-consuming.

Alternately, you can run the same query in your console and all that will be done automatically for you. This saves a lot of time and effort, and this is why almost all developers use the console.

Now that you know the importance of a rails console, here's how you can start it.

Type rails c and this will boot up the console. You can then get into it right away and start coding. This is what your screen should look like:

medium

To close it, hit ctrl+d

One useful feature of the console is the sandbox mode, that rolls back all your modifications when you exit the console. The obvious advantage with this mode is it gives you a temporary environment to try changes and to see their impact on your application, without worrying about altering the application permanently. To use this mode, type
rails c --sandbox and this is how your screen will look:

medium

While creating an email API or module, keep in mind that even when you run it in the sandbox mode, it will still send the email. So, watch out for it, if you don't want the receiver to get your test emails.

Also, when you call external APIs, they are not aware that you are in the sandbox mode, so they can alter your application. So, be more aware of what you call even when you're in the sandbox mode.

And that's your introduction to the Rails console, we'll spend the next several lessons reviewing different ways that we can use the console when building applications.