Essential Rails CLI: The Complete Cheatsheet

on this page · 9 §

Essential Rails CLI: The Complete Cheatsheet

Fast reference for every rails command you'll actually reach for — from spinning up a new project to pushing a production deployment. Covers Rails 7 and 8; a small number of flags are version-specific, so keep rails --version handy.1

All commands below assume you are in the project root and your environment is set up (bundle install done, .env or credentials configured). {: .prompt-tip }

Rails request/response cycle — MVC flow diagram

Creating a New Application

The rails new generator accepts a growing list of flags. Pick only what you need — every skipped component is one less dependency to manage.

bash
# Baseline app with sensible defaults
rails new myapp

# Choose database up front
rails new myapp --database=postgresql
rails new myapp --database=mysql
rails new myapp --database=sqlite3

# API-only (no views, no cookie session)
rails new myapp --api

# Minimal — skips Action Mailer, Active Storage, etc.
rails new myapp --minimal

# Rails 7+ frontend presets
rails new myapp --css=tailwind
rails new myapp --css=bootstrap
rails new myapp --javascript=esbuild
rails new myapp --javascript=importmap   # default in Rails 7

# Common skip flags
rails new myapp --skip-test
rails new myapp --skip-active-storage
rails new myapp --skip-action-mailbox
rails new myapp --skip-action-text

Combining --api --minimal --database=postgresql produces the leanest possible starting point for a JSON service. {: .prompt-info }

Database Operations

The db:* namespace covers every lifecycle stage from provisioning to emergency rollback.

bash
# Core lifecycle
rails db:create          # creates the database(s)
rails db:migrate         # runs all pending migrations
rails db:rollback STEP=1 # reverts the last N migrations
rails db:reset           # drop → create → migrate (destroys data!)
rails db:seed            # loads db/seeds.rb

# Inspection
rails db:migrate:status  # which migrations have/haven't run
rails db:version         # current schema version number

# Schema management
rails db:schema:load     # load from schema.rb (faster than re-running all migrations)
rails db:structure:load  # load from structure.sql (use when db:schema:load isn't enough)
rails db:schema:dump     # regenerate schema.rb from current DB state
rails db:setup           # create + load schema + seed in one step

# Surgical migration control
rails db:migrate:up   VERSION=20240101000000   # run a specific migration
rails db:migrate:down VERSION=20240101000000   # revert a specific migration
Command Destroys data? Notes
db:reset Yes Drop + re-create + migrate
db:schema:load Yes Wipes then loads schema
db:rollback Partial Reverts last migration
db:migrate No Safe to re-run

Generators

Models

bash
# Model with typed attributes
rails generate model Post title:string body:text published:boolean user:references

# Custom primary key type
rails generate model Product id:uuid name:string price:decimal

# Skip test file
rails generate model Tag name:string --skip-test

# Index a column
rails generate model Event slug:string:uniq

Controllers

bash
# Standard controller with action views
rails generate controller Users index show new create edit update destroy

# API controller (no view files)
rails generate controller api/v1/Users --skip-template-engine

# Namespaced
rails generate controller Admin::Dashboard index

# No helper, no assets
rails generate controller Posts --skip-helper --skip-assets

Advanced Generators

bash
# Mailer with action methods
rails generate mailer UserMailer welcome_email password_reset

# Background job
rails generate job ProcessPayment
rails generate job SendNotifications --queue=urgent

# Action Cable channel
rails generate channel Room speak

# Stimulus controller (Rails 7+)
rails generate stimulus form_validation

Testing

bash
# Run everything
rails test

# Scope to a file or directory
rails test test/models/user_test.rb
rails test test/controllers

# Useful flags
rails test -v              # verbose output
rails test --fail-fast     # stop on first failure
rails test --seed=12345    # reproduce a specific random order

# System tests (Capybara + headless browser)
rails test:system
rails test:system -v

# Parallelise across CPU cores
rails test:parallel
rails test:prepare         # set up parallel test databases

Running rails test --fail-fast during TDD keeps the feedback loop tight. Reserve rails test:parallel for CI where full suite time matters. {: .prompt-tip }

Development Tools

bash
# Server
rails server -p 4000        # different port
rails server -b 0.0.0.0     # bind to all interfaces (Docker, Codespaces)
rails server -e production  # production env locally
rails server -d             # run as background daemon

# Console
rails console               # REPL with full app context
rails console --sandbox     # every change is rolled back on exit
rails console -e production # production console (be careful)

# Inspection
rails stats                 # LOC breakdown by layer
rails notes                 # list TODO / FIXME / OPTIMIZE comments
rails middleware            # show the Rack middleware stack
rails about                 # environment info, gem versions

# Routes
rails routes                # all routes
rails routes -g users       # filter by pattern
rails routes -c Users       # filter by controller
rails routes --expanded     # one attribute per line (easier to read)

# Run arbitrary code
rails runner 'puts User.count'

Asset Pipeline

bash
rails assets:precompile     # compile fingerprinted assets for production
rails assets:clean          # remove old compiled versions
rails assets:clobber        # remove all compiled assets

# Propshaft (Rails 8 default) — no compile step needed in dev
# Sprockets (legacy) — same commands, more pre-processing

Production & Deployment

bash
# Credentials
rails credentials:edit                          # edit master credentials
rails credentials:edit --environment production # env-specific credentials
rails credentials:show                          # view decrypted credentials
rails secret                                    # generate a new random secret

# Run in production
rails db:migrate RAILS_ENV=production
rails assets:precompile RAILS_ENV=production

# Cache management
rails dev:cache             # toggle disk caching in development
rails tmp:cache:clear       # clear tmp/cache
rails tmp:clear             # clear all tmp files
rails log:clear             # truncate log files

Rails 7+ Modern Frontend

bash
# Hotwire
rails turbo:install
rails turbo:install:redis   # switch to Redis for Action Cable

# Stimulus
rails stimulus:install

# Import maps (replaces webpack/webpacker)
bin/importmap pin @hotwired/turbo-rails
bin/importmap pin @hotwired/stimulus
bin/importmap pin lodash --download   # vendor locally
bin/importmap json                    # show current pin map

# CSS processors
rails css:install:tailwind
rails css:install:bootstrap
rails css:install:sass

Quick Troubleshooting

bash
# Nuclear reset: wipe everything and start fresh
rails db:drop db:create db:migrate

# Clear junk without touching the database
rails log:clear tmp:clear

# Verify the environment is sane
rails about

# Debug a route 404
rails routes | grep users
# or
rails routes -g users

Always back up production data before running db:reset, db:drop, or any db:migrate in an irreversible direction. {: .prompt-danger }