DEV Community

CalvinJimenez
CalvinJimenez

Posted on

Ruby/Rails Console Commands

Image description

One of the fundamentals of Ruby / Ruby on Rails is the console. Now, the naming scheme for both of these is slightly different with the Ruby console being the Rake and the Rails console being Rails. This comes into play when actually using the console itself as well. To use the rake console you need to go to the console and type in:

rake console
Enter fullscreen mode Exit fullscreen mode

In the rake console you're able to do a lot of things such as test your code with a pry by just writing bending.pry in a method so that when you hit it by running the method you can test what's in the method. To install pry just write

gem install pry
Enter fullscreen mode Exit fullscreen mode

in the console. This is a very useful tool for actually testing any of the code that you have in your method. This is especially useful for when someone doesn't want to have to fully run their application to see if it throws any errors, it's able to single out what an issue is.

Now, when it comes to Rails it's essentially the same thing as the rake console just with a different name. On that note, the equivalent of pry in rails is called byebug. You install it by putting:

gem install byebug
Enter fullscreen mode Exit fullscreen mode

in the console. It's pretty much the same as pry where you just throw it somewhere and run the method you're trying to test to hit the byebug. To actually use the rails console you simply type in:

rails console
Enter fullscreen mode Exit fullscreen mode

Along with these methods of testing the console can be used for so much more. In rails specifically you generate most of you files through the console. Say for example you want to make a new model, you would have to put:

rails g resource [Name of model] (any rows in their tables)
Enter fullscreen mode Exit fullscreen mode

This would generate the model along with any of its needed keys or relations to other tables in your backend. There's a plethora of other things that the rails command can do such as seeding and migrating which is done with:

rails db:migrate
OR
rails db:seed
OR
rails db:migrate db:seed
Enter fullscreen mode Exit fullscreen mode

As you can see there's different ways to make things more efficient when using the console but that's really something you learn and gain skills to over time.

Top comments (0)