skip to content

Outside app/, what do a Rails 8.1 app's bin/, config/, db/, lib/, script/, test/ and vendor/ hold, alongside the Gemfile and Gemfile.lock?

level: juniorimportance: should knowfreq 50%

answer

  1. executables, settings, schema, shared code
  2. lib/tasks loads .rake files
  3. script/ for one-off scripts
  4. test/ mirrors app/
  5. Gemfile.lock pins exact versions

basics

~20 s

In Rails 8.1, bin/ holds executables, config/ settings and routes, db/ migrations and schema, lib/ code outside app/ plus lib/tasks, script/ one-off scripts, test/ the test suite and vendor/ third-party code; the Gemfile declares gems and Gemfile.lock records the exact versions.

solid answer

~40 s

`bin/` holds the app's executables: `rails`, `rake`, `setup`, `dev` and the tool binstubs. `config/` holds `application.rb`, per-environment files, initializers, `routes.rb`, `database.yml`, locales and the YAML for storage, cable, queue and cache. `db/` starts with `seeds.rb` and gains `migrate/` and a schema dump as migrations run, plus `queue_schema.rb`, `cache_schema.rb` and `cable_schema.rb` from the Solid installers. `lib/` is for code that is not part of the app's domain, with `lib/tasks` for `.rake` files the Rakefile loads. `script/` holds one-off scripts and benchmarks. `test/` mirrors `app/` with `models`, `controllers`, `fixtures` and `test_helper.rb`. `vendor/` holds third-party code, such as `vendor/javascript` for import-map downloads. `Gemfile` declares dependencies; `Gemfile.lock` pins what Bundler resolved, and an app commits both.

code

ruby · 7 lines
ruby
# Rakefile
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

require_relative "config/application"

Rails.application.load_tasks

go deeper

for a junior

Recall what each top-level folder is for and that both Gemfile and Gemfile.lock are committed.

for a middle

Explain the wiring: load_tasks for lib/tasks, autoload_lib for the rest of lib/, which db/ files appear when, and how test/ mirrors app/.

for a senior

Use the map to review a codebase quickly: spot code in the wrong folder, a missing lockfile, or tasks and scripts that belong somewhere else.

for a principal

Decide repository conventions that outlast one team, such as what may go in lib/ versus a gem and when vendored code is acceptable.

## The rest of the tree `app/` is where the application's own classes live, but a Rails 8.1 app has a dozen other top-level entries. Each has a single job, and knowing them is what lets you read an unfamiliar repository quickly. ## Folders that hold code and configuration | Folder | Purpose | Created by `rails new` with | |---|---|---| | `bin/` | the app's executables | `rails`, `rake`, `setup`, `dev`, `rubocop`, `brakeman`, `bundler-audit`, `ci`, `thrust`, `docker-entrypoint` | | `config/` | settings, routes, environments, initializers, locales | `application.rb`, `routes.rb`, `database.yml`, `puma.rb`, `storage.yml`, `cable.yml` and more | | `db/` | migrations, schema dumps, seeds | `seeds.rb`; Solid adds `queue_schema.rb`, `cache_schema.rb`, `cable_schema.rb` | | `lib/` | code that is not specific to the app's domain | `lib/tasks/` with a `.keep` file | | `script/` | one-off and general-purpose scripts, benchmarks | an empty folder with `.keep` | | `test/` | the Minitest suite | `controllers/`, `models/`, `helpers/`, `mailers/`, `integration/`, `fixtures/files/`, `test_helper.rb` | | `vendor/` | third-party code kept in the repository | `.keep`; `importmap:install` adds `vendor/javascript` | A few details worth knowing: - **`bin/rails`** is the entry point for every command (`server`, `console`, `generate`, `db:migrate`); the other `bin/` files are binstubs that run a tool with the app's bundle. - **`db/migrate/`** does not exist in a fresh app; the first migration generator creates it, and running migrations writes the schema dump next to it. - **`lib/tasks/`** is wired in: the generated `Rakefile` calls `Rails.application.load_tasks`, which loads every `lib/tasks/**/*.rake` file. In an 8.1 app, `config.autoload_lib(ignore: %w[assets tasks])` also makes the Ruby files elsewhere in `lib/` loadable by constant name. - **`script/`** is where `bin/rails generate script` and `bin/rails generate benchmark` write their files, the latter under `script/benchmarks/`. - **`test/`** mirrors `app/`: `test/models/order_test.rb` tests `app/models/order.rb`. `bin/rails test` runs `test/**/*_test.rb`, leaving out `test/system`, `test/dummy` and `test/fixtures` unless you name them. ## Files at the root - **`Gemfile`** lists the gems the app depends on, with version constraints and groups. - **`Gemfile.lock`** records the exact versions Bundler resolved and the platforms they were resolved for; `rails new` adds `x86_64-linux` (and `aarch64-linux` on an ARM machine) so the lock also works in a Linux container. - **`Rakefile`** loads the app's tasks; **`config.ru`** starts the app under a Rack server; **`Dockerfile`**, **`.dockerignore`**, **`.gitignore`**, **`.rubocop.yml`** and **`README.md`** complete the root. An application commits **both** the Gemfile and the lockfile, so every developer, CI run and production image installs the same versions. (A gem's own repository is a different case.) ## How to use this map in practice 1. A setting changes behaviour app-wide? Look in `config/`. 2. A table or column is in question? Look in `db/` and the schema dump. 3. A command-line task runs on a schedule or by hand? Look in `lib/tasks/` or `script/`. 4. A class is not a model, controller, job or mailer and not app-specific? Consider `lib/`. Most of what lives in these folders has its own detailed rules elsewhere, from environment files to migrations; the directory tree only tells you where to look. ## `config/` at a glance `config/` is the most crowded folder in a new 8.1 app, so it helps to know which file answers which question: - **`routes.rb`**: which URL reaches which controller action. - **`database.yml`**, **`storage.yml`**, **`cable.yml`**: database connections, Active Storage services, Action Cable adapter. - **`queue.yml`**, **`recurring.yml`**, **`cache.yml`**: Solid Queue workers and schedules, Solid Cache limits, written by the Solid installers. - **`importmap.rb`**: the JavaScript pins, written by `importmap:install`. - **`puma.rb`**, **`deploy.yml`**, **`ci.rb`**, **`bundler-audit.yml`**: the web server, Kamal, local CI steps and the audit ignore list. - **`environments/`**, **`initializers/`**, **`locales/`**: per-environment settings, boot-time code and translations. - **`credentials.yml.enc`** is committed; the **`master.key`** that decrypts it is added to `.gitignore` when it is generated.

  • Why does a fresh Rails 8.1 app have no `db/migrate` folder or `db/schema.rb`?
    `rails new` only writes `db/seeds.rb` (plus the Solid schema files from their installers). `db/migrate` appears when the first migration is generated, and the primary schema dump is written when migrations run. A repository with migrations but no schema dump, or the reverse, is worth asking about.
  • Should a Rails app commit `Gemfile.lock`?
    Yes. An application commits the lockfile so development, CI and production install exactly the same gem versions; `rails new` even adds Linux platforms to it so a container build resolves the same set. Libraries published as gems are the case where practice differs.

  • lib/tasks files must be required from the Rakefile one by one.
  • Gemfile.lock is a build artefact and belongs in .gitignore.
  • bin/ is a place for compiled binaries and is ignored by git.
  • A fresh app already contains db/migrate and db/schema.rb.
  • bin/rails test runs the system tests along with everything else.