Rails Quick Reference for Experienced Programmers

For more see:

MVC Architecture

Convention Over Configuration

Key Components

Routes Conventions

# config/routes.rb
resources :posts  # Creates 7 RESTful routes:
HTTP Verb Path Controller#Action Purpose
GET /posts posts#index List all
GET /posts/new posts#new New form
POST /posts posts#create Create
GET /posts/:id posts#show Show one
GET /posts/:id/edit posts#edit Edit form
PATCH/PUT /posts/:id posts#update Update
DELETE /posts/:id posts#destroy Delete
# Nested resources
resources :posts do
  resources :comments  # /posts/:post_id/comments
end

# Custom routes
get 'about', to: 'pages#about'
root 'posts#index'  # Homepage

# Namespace
namespace :admin do
  resources :users  # /admin/users → Admin::UsersController
end

CLI Essentials

rails new myapp          # Scaffold new app
rails generate model User name:string email:string
rails generate controller Users
rails db:migrate         # Run pending migrations
rails console            # IRB with app loaded
rails server             # Start dev server (port 3000)
rails routes             # Show all routes

Migration Commands

rails db:migrate                    # Run all pending migrations
rails db:rollback                   # Undo last migration
rails db:rollback STEP=3            # Undo last 3 migrations
rails db:migrate:down VERSION=20240101120000  # Undo a specific migration
rails db:migrate:up VERSION=20240101120000    # Run a specific migration
rails db:migrate VERSION=20240101120000       # Migrate to a specific version (up or down)
rails db:migrate:status             # Show status of all migrations (up/down)
rails db:migrate:redo               # Rollback then re-run last migration
rails db:migrate:redo STEP=3        # Rollback then re-run last 3 migrations
rails db:reset                      # Drop, recreate, and re-migrate the database
rails db:seed                       # Run db/seeds.rb

Directory Layout

ActiveRecord Patterns

User.create(name: "Tom")
User.find(1)
User.where(admin: true).first
user.posts  # Association from has_many :posts

Associations

class User < ApplicationRecord
  has_many :posts                    # user.posts → looks for user_id in posts table
  has_many :comments, through: :posts # user.comments via join
  has_one :profile                   # user.profile → looks for user_id in profiles table
  has_and_belongs_to_many :roles     # join table: roles_users (alphabetical)
end

class Post < ApplicationRecord
  belongs_to :user                   # post.user → requires user_id column on posts table
  belongs_to :author, class_name: "User", foreign_key: "author_id"  # custom name
  has_many :comments, dependent: :destroy  # delete comments when post is deleted
end

Column Naming Conventions

Common Association Options

Option Example Purpose
dependent has_many :posts, dependent: :destroy What happens to children when parent is deleted (:destroy, :nullify, :restrict_with_error)
foreign_key belongs_to :author, foreign_key: "author_id" Specify non-conventional FK column
class_name belongs_to :author, class_name: "User" When association name differs from class
through has_many :tags, through: :taggings Join through another association
inverse_of has_many :posts, inverse_of: :author Helps Rails recognize bidirectional associations
optional belongs_to :team, optional: true Allow nil (belongs_to is required by default in Rails 5+)
counter_cache belongs_to :user, counter_cache: true Cache count in parent (needs posts_count column on users)

Migration for Associations

# Generate a migration that adds a foreign key
rails generate migration AddUserToPosts user:references

# Produces:
class AddUserToPosts < ActiveRecord::Migration[7.0]
  def change
    add_reference :posts, :user, null: false, foreign_key: true
    # Creates user_id column + index + DB-level foreign key constraint
  end
end

Modern Rails (~7.x)