Your Go-To Rails Command Line Cheat Sheet!

I’ve been learning Rails and working in backend development environments for the last few weeks through my coursework at Flatiron school, and I’ve found there are some commands used more frequently than others when building out Rails applications.

I t…

I’ve been learning Rails and working in backend development environments for the last few weeks through my coursework at Flatiron school, and I’ve found there are some commands used more frequently than others when building out Rails applications.

I thought it would be beneficial to myself as well as others to create a ‘cheat sheet’ that lists all the most commonly used commands while learning Rails and developing a backend/full stack skill set. I’ve broken them down into the following:



Getting Started

# if the gem doesn't yet exist in your ruby application:
gem install rails

# generally run first thing, to install all dependencies
bundle install

# if you modify your gem file and need to update
# your installed dependencies
bundle update

# creates a new rails application in the current
# directory called ingenious_app
rails new ingenious_app



Basic Commands

Some ruby commands can be shortened. If an abbreviated command (or “shortcut alias”) exists, it is listed as a comment right below the full command.

# lists the current version of rails being used
rails version
# rails -v

# runs a web server so that you may access the database
# using the default url http://localhost:3000
rails server
# rails s
# to exit, use Ctrl-C

# opens a console session to play/work with the data,
# test various ideas, check your work, etc.
rails console
# rails c
# to exit, use Ctrl-D

# lists all active routes that exist in the rails application
rails routes

# generates new code for you - more on this command in a bit
rails generate
# rails g

# produces an abbreviated list of available commands,
# most lack any description
rails -h

# add -h to any command to learn more about possible options
rails <command> -h
# example:
# rails generate -h

# produces a detailed list of available commands
# with descriptions
rails --tasks



Database (db) Commands

# creates the database for the current environment (ex: development)
rails db:create

# deletes the database for the current environment
rails db:drop

# runs any pending migrations for the current environment
rails db:migrate

# checks the status of all migrations (up, down, or pending)
rails db:migrate:status

# rolls back (undo) the most recent migration
rails db:rollback

# runs the db/seed.rb file, thereby seeding the database
rails db:seed

# truncates all tables in the current database and re-runs the seed file (empties the database, replaces with seed data)
rails db:seed:replant

# runs multiple commands, namely db:drop and db:setup; resets the database by deleting the database, loading the current schema into the current environment database, and then running the seed file
rails db:reset



Generator Commands

Generators were mentioned above in the basic commands section, but due to the robust nature of generators, they merit their own section. For these commands, it will be easier to explain using an example. We’ll work with a Users model that requires a name and an age.

# generates a User model where relationships,
# custom methods, and validations can be defined.
# Specify desired columns and their data types. A column's
# data type defaults to string unless otherwise specified
rails g model user name age:integer

# creates the following User model
class User < ApplicationRecord
end

# as well as the following migration file that creates
# the Users table with columns for name and age.
class CreateUsers <ActiveRecord::Migration[6.1]
    def change
        create_table :users do |t|
            t.string :name
            t.integer :age

            t.timestamps
        end
    end
end
# Generates a UsersController where CRUD route methods
# and supporting private methods are defined
rails g controller users index show

# Any methods you list after the controllers name pre-build
# them for you. The above creates the following:
class UsersController < ApplicationController
    def index
    end

    def show
    end
end

Note: The following command relies on having the active_model_serializers gem installed. Serializers allow you to define what data gets generated in a JSON using attributes and relationships.

# Generates a UserSerializer
rails g serializer user name age

# creates the following:
class UserSerializer < ActiveModel::Serializer
    attributes :id, :name, :age
end
# creates a migration file that will need filling in
rails g migration create_users name age:integer

# creates the following migration;
# note that including columns does nothing:
class CreateUsers <ActiveRecord::Migration[6.1]
    def change
    end
end

Want something that does (almost) everything? Resource is the command you want!

# creates a migration with create_table and columns
# defined, the users controller, the user model,
# AND the user serializer. Woah!
rails g resource user name age:integer



Seeking Reader Input

Is this cheat sheet missing anything you think should be included? Share out in the discussion below and I’ll modify this post as needed!


Print Share Comment Cite Upload Translate
APA
Alex Wentz | Sciencx (2024-04-18T16:48:19+00:00) » Your Go-To Rails Command Line Cheat Sheet!. Retrieved from https://www.scien.cx/2021/12/13/your-go-to-rails-command-line-cheat-sheet/.
MLA
" » Your Go-To Rails Command Line Cheat Sheet!." Alex Wentz | Sciencx - Monday December 13, 2021, https://www.scien.cx/2021/12/13/your-go-to-rails-command-line-cheat-sheet/
HARVARD
Alex Wentz | Sciencx Monday December 13, 2021 » Your Go-To Rails Command Line Cheat Sheet!., viewed 2024-04-18T16:48:19+00:00,<https://www.scien.cx/2021/12/13/your-go-to-rails-command-line-cheat-sheet/>
VANCOUVER
Alex Wentz | Sciencx - » Your Go-To Rails Command Line Cheat Sheet!. [Internet]. [Accessed 2024-04-18T16:48:19+00:00]. Available from: https://www.scien.cx/2021/12/13/your-go-to-rails-command-line-cheat-sheet/
CHICAGO
" » Your Go-To Rails Command Line Cheat Sheet!." Alex Wentz | Sciencx - Accessed 2024-04-18T16:48:19+00:00. https://www.scien.cx/2021/12/13/your-go-to-rails-command-line-cheat-sheet/
IEEE
" » Your Go-To Rails Command Line Cheat Sheet!." Alex Wentz | Sciencx [Online]. Available: https://www.scien.cx/2021/12/13/your-go-to-rails-command-line-cheat-sheet/. [Accessed: 2024-04-18T16:48:19+00:00]
rf:citation
» Your Go-To Rails Command Line Cheat Sheet! | Alex Wentz | Sciencx | https://www.scien.cx/2021/12/13/your-go-to-rails-command-line-cheat-sheet/ | 2024-04-18T16:48:19+00:00
https://github.com/addpipe/simple-recorderjs-demo