skip to content

Command Line Toolkit

bin/rails is one entry point for servers, consoles, generators and tasks, and an app adds its own Rake tasks and Thor generators. Interviewers check daily fluency with it and the generator system.

part ofRuby on Railsread it →
on this pageshow

explore

questions

14

In a Rails 8.1 app, what do bin/rails server, console, dbconsole and runner each start, and when do you reach for each?

level: juniorimportance: must knowfreq 68%

answer

  1. four entry points, one application
  2. s, c, db and r aliases
  3. Puma on port 3000, localhost in development
  4. the database's own client, not Ruby
  5. code string, file path or - for stdin

basics

~20 s

bin/rails server boots the app under Puma on port 3000; console opens IRB with the app loaded; dbconsole opens the database's own client (psql, mysql, sqlite3) from database.yml; runner boots the app, runs one Ruby string or file, and exits.

solid answer

~50 s

All four live in `railties/lib/rails/commands` and share short aliases: `s`, `c`, `db` and `r`. `bin/rails server` boots the application under Puma, on port 3000 and bound to `localhost` in development; `-p` changes the port, `-b` the address, `-e` the environment. `bin/rails console` boots the same app inside IRB so you can call models, jobs and helpers by hand. `bin/rails dbconsole` reads `config/database.yml` and execs the adapter's own client (`psql`, `mysql`, `sqlite3`), so you type SQL, not Ruby; `--db` picks one of several databases and `-p` passes the password along. `bin/rails runner` boots the app, evaluates a code string, a `.rb` file or stdin (`-`), and exits, which makes it the fit for a one-off script or a cron line. Choose by what you need: HTTP traffic, a Ruby prompt, a SQL prompt, or a non-interactive script.

code

bash · 5 lines
bash
bin/rails s -p 4000 -b 0.0.0.0          # serve on all interfaces, port 4000
bin/rails c                              # IRB with the app loaded
bin/rails db --db queue                  # sqlite3/psql on the queue database
bin/rails r 'puts Rental.overdue.count'  # run once and exit
bin/rails r script/close_season.rb 2026  # 2026 lands in ARGV

go deeper

for a junior

Know the four commands, their aliases s, c, db and r, and that server defaults to Puma on port 3000. Be clear that console is a Ruby prompt and dbconsole a SQL prompt.

for a middle

Explain the flags you actually use: -p, -b and -e on server, --db and -p on dbconsole, and runner's three input forms. Know why a development server is unreachable from another device.

for a senior

Show you pick runner for scheduled one-off work, know what the executor wrapper means for a long script, and avoid opening dbconsole against production when a scoped console query would do.

for a principal

Frame these commands as the team's operational surface: which ones are allowed against production, how access is audited, and when a recurring runner script should become a Rake task or a job.

## One executable, many subcommands Every Rails app carries `bin/rails`, a three-line script that sets `APP_PATH`, loads `config/boot.rb` and hands the arguments to `rails/commands`. That file expands a handful of **aliases** before dispatching: | Alias | Command | What it starts | |---|---|---| | `s` | `server` | the web server, serving HTTP | | `c` | `console` | an IRB session with the app loaded | | `db` | `dbconsole` | the database's own command-line client | | `r` | `runner` | a non-interactive Ruby script, then exit | (`g`, `d` and `t` exist too, for generators, destroy and tests.) All four commands below boot the application, so they see the same models, initializers and credentials the web process sees. ## bin/rails server `server` builds a `Rails::Server` and runs the app through a Rack handler, which is **Puma** in a generated app. Defaults worth knowing: - **Port 3000**, unless `-p` / `--port` or the `PORT` environment variable says otherwise. - **Bind address**: `localhost` in development and `0.0.0.0` in every other environment, unless `-b` / `--binding` or `BINDING` overrides it. That is why a dev server inside a VM or container is unreachable from the host until you pass `-b 0.0.0.0`. - **Environment**: development unless `-e` or `RAILS_ENV` names another. - In development it writes its PID to `tmp/pids/server.pid` and logs to stdout. In production the same command is what the container runs behind Thruster; Puma's own settings live in `config/puma.rb`, which is a separate topic. ## bin/rails console `console` boots the app and opens **IRB** with Rails' helpers registered: `reload!`, `app` (an integration session), `helper` (the view helpers) and `controller`. The prompt shows the app name and a coloured environment tag (`dev`, `test`, `prod`). `--sandbox` (`-s`) wraps the session's database work in a transaction that is never committed. This is where you poke at a model, try a query or enqueue a job by hand. ## bin/rails dbconsole `dbconsole` does **not** start Ruby for you to type into. It finds the database configuration for the current environment and execs that adapter's client with the host, port, user and database filled in: - `psql` for PostgreSQL, `mysql` for MySQL and MariaDB, `sqlite3` for SQLite; - `--db` / `--database` chooses one entry when the environment has several (a Rails 8.1 production `database.yml` has `primary`, `cache`, `queue` and `cable`); - `-p` / `--include-password` passes the password from `database.yml` instead of prompting; - `--mode` and `--header` tune the SQLite client's output. Use it when SQL is the right tool: `\d rentals` in psql, an `EXPLAIN`, a quick count without loading models. ## bin/rails runner `runner` boots the app, runs code, and exits. It accepts three forms: 1. a **code string**: `bin/rails runner 'puts Rental.overdue.count'`; 2. a **file path**: `bin/rails runner script/close_season.rb`, with any further arguments left in `ARGV`; 3. **`-`** to read the script from stdin. By default the code runs inside `Rails.application.executor.wrap`, the same unit-of-work wrapper a request gets; `--skip-executor` (`-w`) opts out for long-running scripts. Its USAGE text even shows runner as a shebang line. Reach for it for cron jobs and one-off maintenance that does not deserve a Rake task. ## Choosing between them - Need to **serve requests**? `server`. - Need to **explore or fix data interactively in Ruby**? `console`, ideally `--sandbox` while you experiment. - Need **raw SQL** with the database's own tooling? `dbconsole`. - Need to **run Ruby once, unattended**? `runner`. - Need to **boot and exit**, only to prove the app loads? `bin/rails boot` exists for exactly that and runs no code of yours. The common confusions are mixing up `console` and `dbconsole` (Ruby prompt vs SQL prompt) and expecting `runner` to leave a prompt open; it never does. ## Mistakes interviewers listen for - **Using `dbconsole` to call models.** It is the database's client; `Rental.count` there is a SQL syntax error. Use `console`. - **Scripting the console.** Piping commands into `bin/rails console` is fragile; `runner` is the supported non-interactive path, and an exception in the script ends the process with a failing status that cron or CI can see. - **Forgetting the environment.** All four commands default to development unless `RAILS_ENV` or `-e` says otherwise, so a script on a server needs the environment stated. - **Forgetting `--db` on a multi-database app.** Without it, `dbconsole` opens the first configuration listed for the environment (normally `primary`), so the Solid Queue tables are not there; pass `--db queue`. - **Assuming `bin/rails server` and production boot differ.** The server command is a thin wrapper over the same Rack app the container's Puma serves; behaviour differences come from the environment, not the command.

  • Why can't a phone on the same Wi-Fi reach bin/rails server running on your laptop in development?
    In development `bin/rails server` binds to `localhost` unless told otherwise, so only the laptop itself can connect. Pass `-b 0.0.0.0` (or set `BINDING`) to listen on every interface. Other environments already default to `0.0.0.0`. Development's default `config.hosts` admits any IP address, so browsing to the laptop's IP works; a custom host name such as `laptop.local` must be added to `config.hosts`.
  • What does bin/rails runner's --skip-executor flag turn off?
    By default runner wraps the script in `Rails.application.executor.wrap`, the same unit-of-work wrapper each request gets, so autoloading and framework hooks behave as they do in a request; Active Record, for example, enables its query cache for the duration. A script that runs for hours is one huge unit of work under that wrapper, so `--skip-executor` (`-w`) runs it unwrapped and leaves the script to manage such things itself.

  • bin/rails dbconsole opens an IRB session with the models loaded
  • bin/rails runner leaves an interactive prompt open after the script ends
  • bin/rails server listens on 0.0.0.0 by default in development
  • The -p flag of bin/rails dbconsole sets the database port
  • bin/rails boot runs a script after loading the application
open as a page

In a Rails 8.1 app, where do custom Rake tasks live, and what does bin/rails generate task loyalty recompute write?

level: juniorimportance: must knowfreq 58%

basics

~20 s

Custom tasks live in .rake files under lib/tasks, which the app's Rakefile loads through Rails.application.load_tasks. bin/rails generate task loyalty recompute writes lib/tasks/loyalty.rake with a loyalty namespace and a recompute task stub that has desc "TODO" and the :environment prerequisite.

open as a page

In Rails 8.1, what do bin/rails generate model, controller, resource and scaffold each write, and when do you pick resource over scaffold?

level: juniorimportance: must knowfreq 72%

basics

~20 s

model writes a migration, model class, model test and fixture; controller writes named actions, their views, a helper, a test and get routes; resource adds an empty controller and resources route to the model files; scaffold adds a full CRUD controller, ERB views and JSON views.

open as a page

In Rails 8.1, how do RAILS_ENV and the -e flag decide which environment bin/rails console, runner and server boot into?

level: middleimportance: must knowfreq 55%

basics

~20 s

Without a flag, bin/rails uses RAILS_ENV, then RACK_ENV, then development. The -e flag on server, console, runner and dbconsole overrides that for one command; Rake tasks such as db:migrate have no -e, so they need RAILS_ENV=production in front.

open as a page

In a Rails 8.1 Rake task, what does the :environment prerequisite actually load, and what changes when the task also takes arguments?

level: middleimportance: must knowfreq 50%

basics

~20 s

The Rakefile only loads config/application.rb; the :environment prerequisite runs Rails' environment task, which requires config/environment.rb and initializes the app, without eager loading by default. With arguments the form is task :recompute, [:store_id] => :environment, called as bin/rails "loyalty:recompute[42]".

open as a page

In Rails 8.1, what does bin/rails generate model Kit name:string! 'price:decimal{10,2}' supplier:references sku:string:uniq write into the migration and model?

level: middleimportance: must knowfreq 50%

basics

~20 s

The migration gets name with null: false, price as decimal precision 10 scale 2, a supplier reference with null: false and foreign_key: true, and a unique index on sku; the model gets only belongs_to :supplier, and nothing validates name or sku.

open as a page

In Rails 8.1, how does bin/rails tell its own commands from Rake tasks, and how do bin/rails --help and bin/rails -T differ?

level: middleimportance: should knowfreq 30%

basics

~20 s

bin/rails first looks for a Rails command class with that name (server, console, stats, notes, about); if none exists it hands the name to Rake. --help lists commands and framework tasks but hides the app's own lib/tasks; -T lists every described Rake task.

open as a page

In the Rails 8.1 console, what do reload!, app and helper give you, and what catches people out with each?

level: middleimportance: should knowfreq 42%

basics

~20 s

reload! reloads changed application code, but objects already in variables keep their old class; app is an integration session that can call route helpers and send requests without a server; helper is ApplicationController's view-helper proxy for trying formatting helpers.

open as a page

In Rails 8.1, what does bin/rails destroy scaffold Kit undo and leave behind, and what do --pretend, --skip and --force change?

level: middleimportance: should knowfreq 38%

basics

~20 s

destroy replays the same generators in reverse: it deletes the files scaffold created and removes the resources :kits line. It does not roll back a migration you already ran, and it deletes files you edited. --pretend previews, --skip keeps existing files, --force overwrites them.

open as a page

When you must inspect live data in a ski-rental Rails 8.1 app, what does bin/rails console --sandbox protect against, and what does it leave exposed?

level: seniorimportance: should knowfreq 38%

basics

~20 s

--sandbox opens a transaction on every database connection the console checks out and never commits it, so row changes vanish on exit. Locks, after_commit side effects, emails, HTTP calls and heavy reads still reach production.

open as a page

In a coffee-chain Rails 8.1 app, should a one-off backfill that recomputes every customer's loyalty points live in a migration or a Rake task, and why?

level: seniorimportance: should knowfreq 45%

basics

~20 s

Put it in a Rake task. Migrations should describe schema and run at deploy time, inside a transaction on PostgreSQL, and since Rails 8.0 a fresh database loads schema.rb instead of replaying them. A task can be batched, idempotent, rerun, dry-run first and deleted once done.

open as a page

How do you build a custom Rails 8.1 generator with bin/rails generate generator, and what do NamedBase, source_root and Thor actions give you?

level: seniorimportance: should knowfreq 25%

basics

~20 s

bin/rails generate generator reward writes lib/generators/reward/ with a RewardGenerator subclass of Rails::Generators::NamedBase, a USAGE file, a templates folder and a test. NamedBase supplies the name argument and helpers such as file_name; source_root points at templates; each public method runs Thor actions in order.

open as a page

In a Rails 8.1 app, how do config.generators and lib/templates let a team change what bin/rails generate writes by default?

level: seniorimportance: should knowfreq 28%

basics

~10 s

config.generators in config/application.rb sets defaults for every generator run, such as UUID keys, no helpers or a different test framework. Files under lib/templates override a built-in generator's templates, for example lib/templates/erb/scaffold/index.html.erb.tt.

open as a page

What is a Rails application template, and how does bin/rails app:template apply one to an existing Rails 8.1 app?

level: middleimportance: nice to knowfreq 16%

basics

~10 s

An application template is a Ruby script run with Rails' generator actions (gem, generate, route, initializer, rails_command). bin/rails app:template LOCATION=path-or-URL applies it to an existing app, then runs bundle install and the after_bundle blocks.

open as a page