skip to content

Default Directory Tree

A fresh Rails app puts every file in a fixed place: app/ by role, config/, db/, lib/ and lib/tasks, storage/, tmp/ and test/. Interviewers check you know where code belongs and where new folders go.

part ofRuby on Railsread it →
on this pageshow

explore

questions

5

In a Rails 8.1 app, what does each folder under app/ hold, and which base classes does rails new put there?

level: juniorimportance: must knowfreq 75%

answer

  1. folders by role, not by feature
  2. Application* base class per framework
  3. ApplicationRecord with primary_abstract_class
  4. app/javascript from the installers
  5. views/layouts and views/pwa

basics

~20 s

Rails 8.1's app/ is split by role: models, controllers, views, helpers, mailers, jobs, assets and javascript. rails new seeds each Ruby folder with an Application* base class, such as ApplicationRecord and ApplicationController, that the app's own classes inherit from.

solid answer

~30 s

Under `app/` each folder holds one kind of object: `models/` (Active Record and plain domain classes), `controllers/`, `views/` (templates and layouts), `helpers/` (view helper modules), `mailers/`, `jobs/`, `assets/` (stylesheets and images served by Propshaft) and `javascript/` (import-map entry point and Stimulus controllers). `rails new` writes one base class per framework: `ApplicationRecord < ActiveRecord::Base` marked `primary_abstract_class`, `ApplicationController < ActionController::Base` with `allow_browser versions: :modern`, `ApplicationJob`, `ApplicationMailer` with `layout "mailer"`, and the `ApplicationHelper` module. Your classes inherit from these, so app-wide behaviour goes in one place. It also writes `views/layouts/application.html.erb`, the mailer layouts and the PWA files in `views/pwa`, plus empty `controllers/concerns` and `models/concerns`.

code

ruby · 10 lines
ruby
# app/models/application_record.rb
class ApplicationRecord < ActiveRecord::Base
  primary_abstract_class
end

# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  # Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has.
  allow_browser versions: :modern
end

go deeper

for a junior

Recall each app/ folder and its role, and that every model, controller, job and mailer inherits from a generated Application* parent.

for a middle

Explain why the base classes exist, what primary_abstract_class does for ApplicationRecord, and how skipped frameworks remove their folders.

for a senior

Judge when a new role folder such as app/services earns its place versus keeping plain classes in app/models, and keep the layout predictable for newcomers.

for a principal

Set conventions for how app/ grows across teams so that ten developers still find code where the layout promises it.

## Organised by role A Rails app keeps almost all of its own code under **`app/`**, and the first level of that folder is organised by **role**, not by feature. A blog's post-related code is spread across `app/models/post.rb`, `app/controllers/posts_controller.rb` and `app/views/posts/`, and every developer who has seen one Rails app knows where to look in the next one. That predictability is the point of the layout. ## The folders a Rails 8.1 app starts with | Folder | Holds | Seeded with | |---|---|---| | `app/models` | Active Record models and plain domain classes | `application_record.rb`, `concerns/` | | `app/controllers` | controllers that handle requests | `application_controller.rb`, `concerns/` | | `app/views` | templates, partials, layouts | `layouts/application.html.erb`, `layouts/mailer.*.erb`, `pwa/` | | `app/helpers` | modules of methods for views | `application_helper.rb` | | `app/mailers` | Action Mailer classes | `application_mailer.rb` | | `app/jobs` | Active Job classes | `application_job.rb` | | `app/assets` | stylesheets and images for Propshaft | `stylesheets/application.css`, `images/` | | `app/javascript` | JavaScript entry point and Stimulus controllers | written by `importmap:install` and `stimulus:install` | Skipping a framework removes its folder: `--skip-active-job` drops `app/jobs`, `--skip-action-mailer` drops `app/mailers` and the mailer layouts. ## The base classes and why they exist `rails new` writes one abstract parent per framework so that app-wide behaviour has a single home: - **`ApplicationRecord < ActiveRecord::Base`**, marked with `primary_abstract_class`, so it has no table of its own and every model inherits its connection settings. - **`ApplicationController < ActionController::Base`**, which in 8.1 starts with `allow_browser versions: :modern` and, in import-map apps, `stale_when_importmap_changes`. - **`ApplicationJob < ActiveJob::Base`**, where retry and discard rules shared by every job can go. - **`ApplicationMailer < ActionMailer::Base`**, with `default from: "[email protected]"` and `layout "mailer"`. - **`ApplicationHelper`**, a module rather than a class, for helpers every view can use. Generators follow the same convention: `bin/rails generate model Order` writes `class Order < ApplicationRecord`, not `< ActiveRecord::Base`. ## Folders that appear later - `app/channels` arrives when you generate an Action Cable channel; a new 8.1 app does not have one. - `app/javascript/controllers` holds Stimulus controllers; `stimulus:install` seeds it with `application.js`, `index.js` and `hello_controller.js`. - Folders you create yourself, such as `app/services` or `app/pdfs`, sit beside the generated ones. Rails picks up every subfolder of `app/` that exists at boot (except `assets`, `javascript` and `views`) as a root for constant lookup, so a new folder needs one server restart and no configuration. - The `concerns/` folders exist from the start, empty, as the home for shared modules. ## What an interviewer listens for 1. **Role-based placement**: a candidate who says "the controller goes in app/controllers, its templates in app/views/orders" has the model of the layout. 2. **Base classes**: knowing that models inherit from `ApplicationRecord` and controllers from `ApplicationController`, and why that indirection exists. 3. **Not everything is a model**: `app/models` may hold plain Ruby classes that are not tables, and adding a role folder is normal when one kind of object multiplies. ## Where things do not go Several habits from older Rails versions or other stacks put files in the wrong place: - **JavaScript in `app/assets/javascripts`**: that was the Sprockets layout. A Rails 8.1 import-map app keeps its JavaScript in `app/javascript`, and Propshaft does not compile JavaScript. - **Hand edits in build output**: `app/assets/builds` appears when a CSS or JS bundler is installed (for example by `tailwindcss-rails` or `cssbundling-rails`); its contents are build output and are ignored by git. - **Templates beside their classes**: a mailer's or controller's templates live under `app/views/<name>/`, not next to the Ruby file. - **Shared view code in controllers**: formatting methods for templates belong in `app/helpers`, which every view can call, rather than in controller methods exposed to views.

  • Why do models inherit from `ApplicationRecord` instead of `ActiveRecord::Base` directly?
    `ApplicationRecord` is the app's own abstract base, marked `primary_abstract_class`, so behaviour every model should share, such as a common scope or connection settings, can live in one class without monkey-patching `ActiveRecord::Base`. Gems and engines that also subclass `ActiveRecord::Base` are not affected by what the app adds there.
  • Where do a model's templates go if it has no controller, such as a mailer for orders?
    Mailer templates live under `app/views` too, in a folder named after the mailer, such as `app/views/order_mailer/confirmation.html.erb`, rendered inside `app/views/layouts/mailer.html.erb`. The mailer class itself sits in `app/mailers`, inheriting from `ApplicationMailer`.

  • app/models may only contain Active Record classes backed by tables.
  • Generated models inherit directly from ActiveRecord::Base.
  • A new Rails 8.1 app has an app/channels folder by default.
  • JavaScript files go in app/assets/javascripts for the asset pipeline.
  • Helpers are classes that controllers instantiate per request.
open as a page

In a Rails 8.1 print-shop app, where should a class that builds PDF invoices live: app/models, a new app/services folder, or lib/?

level: middleimportance: must knowfreq 55%

basics

~20 s

Domain code goes under app/: a plain class in app/models or a role folder such as app/services or app/pdfs; lib/ is for code that is not about the print shop and could become a gem. A new app/ folder needs no configuration, only a restart.

open as a page

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%

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.

open as a page

In a Rails 8.1 app, where does a one-off script that imports a print shop's legacy orders belong: script/, lib/tasks/ or db/migrate/?

level: middleimportance: should knowfreq 40%

basics

~10 s

A one-off import belongs in script/, for example script/import/legacy_orders.rb, which loads config/environment and runs with bin/rails runner; lib/tasks/ suits tasks you rerun by name, and db/migrate/ is for schema changes every environment must replay.

open as a page

In a containerised Rails 8.1 app, which of storage/, tmp/, log/ and public/assets must survive a redeploy, and which can be thrown away or rebuilt?

level: seniorimportance: should knowfreq 30%

basics

~10 s

In a Rails 8.1 container, storage/ must persist on a volume because it holds the SQLite databases and Disk-service uploads; tmp/ and log/ are disposable; public/assets is rebuilt by assets:precompile during the image build.

open as a page