Your Go-To Rails Command Line Cheat Sheet!
Tags: ruby, rails, database
Published: 2021-12-13 18:37:46

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](#getting-started) 
- [Basic Commands](#basic)
- [Db Commands](#db)
- [Generator Commands](#generator)

### Getting Started <a name="getting-started"></a>

```ruby
# 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 <a name="basic"></a>

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.

```ruby
# 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 <a name="db"></a>

```ruby
# 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, re-runs the
# seed file (empties the database, replaces with seed data)
rails db:seed:replant

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

### Generator Commands <a name="generator"></a>

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.

```ruby
# 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
```

```ruby
# 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.

```ruby
# Generates a UserSerializer
rails g serializer user name age

# creates the following:
class UserSerializer < ActiveModel::Serializer
    attributes :id, :name, :age
end
```

```ruby
# 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!
```ruby
# 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!