guest@websec: ~/websec/r02-rails-commands — zsh — 100×40 by SaKKo YouTube ENTH

guest@websec ~ % man r02-rails-commands

Rails Commands

You will type these every day. Press ▶ run on each terminal to watch the command and its real-looking output.

Always run bin/rails from the project root so Bundler uses the exact Rails version in Gemfile.lock. Use console --sandbox when poking at real data, fix the parent class right after generating a controller, and keep one encrypted credentials file per environment.
Always run bin/rails from the project folder. It uses the project's own Rails version from the Gemfile, not whatever is installed globally. rails alone works too inside a project, because it hands off to bin/rails.

rails server

Starts Puma on port 3000 and reloads your code on every request in development.

rails console

An interactive Ruby session with your whole app loaded. The fastest way to try a query or check data.

rails generate model

Creates the model, its migration, a test and a fixture in one go. Field syntax is name:type:modifier.

typeuse for
textnames, descriptions. We prefer text over string on Postgres (no length limit, same speed).
booleanflags; always give a default: is_admin:boolean then edit to default: false
integer / decimalcounts / money (price:decimal{12,2})
datetime / datetimestamps like deleted_at, locked_at
jsonbraw external payloads you keep as-is
referencesforeign keys: user:references
:index / :uniqmodifiers that add an index / a unique index

rails generate migration

For changing a table that already exists. Rails reads the name: AddXToY and RemoveXFromY pre-fill the body.

rails generate controller

rails destroy

Undo a generator: removes exactly the files it created. Run it before migrating, or roll back first.

rails routes

Lists every route with its helper name. Filter with -g (grep) or -c (controller). Lesson r09 explains how to read it.

rails test

rails credentials, one file per environment

Secrets (service-account passwords, JWT secrets, API keys) live encrypted in the repo. We don’t use a single master.key: each environment has its own encrypted file and its own key, so a developer with the development key can’t read the secrets of the dev server.

config/credentials/
├── development.yml.enc   # committed: encrypted secrets for your laptop (RAILS_ENV=development)
├── development.key       # NOT committed: decrypts development.yml.enc
├── dev.yml.enc           # committed: our shared dev server (RAILS_ENV=dev)
├── dev.key               # NOT committed: only people who deploy dev have it
├── test.yml.enc          # committed: fake values for bin/rails test
└── test.key              # NOT committed: on every laptop and in CI secrets
which file does Rails read?pick an environment
Rails.application.credentials.dig(:directory, :username)  # nil if missing
Rails.application.credentials.user_jwt_secret            # top-level key
Rails.application.credentials.user_jwt_secret!           # raises KeyError if blank
Never commit a .key file, paste one into chat, or put one in a Docker image. Check with git check-ignore -v config/credentials/dev.key. If a key leaks, rotate every secret inside that file, then re-encrypt with a new key.

rails runner

Cheat sheet

commandwhat it does
bin/rails sstart the server
bin/rails c [--sandbox]console (sandbox rolls back)
bin/rails g model Name field:typemodel + migration + test + fixture
bin/rails g migration AddXToY x:typeschema change for an existing table
bin/rails g controller api/v1/admin/thingscontroller + test (then fix the parent class)
bin/rails destroy …undo a generator
bin/rails db:migrate / db:rollbackapply / undo migrations (r03)
bin/rails routes -g namefind routes and helper names (r09)
bin/rails test [file[:line]]run tests (r10)
bin/rails credentials:edit -e devedit one environment's encrypted secrets
bin/rails runner 'code'run Ruby with the app loaded

Check yourself

You want to try deleting some real records in the console to see what happens, without keeping the change. What do you run?

--sandbox rolls back everything when you exit.