skip to content

Doctrine & Naming Rules

Rails trades configuration for convention: an Article model maps to the articles table, ArticlesController and app/models/article.rb. Interviewers test the doctrine and when to bend it.

part ofRuby on Railsread it →
on this pageshow

explore

questions

6

What do convention over configuration and DRY mean in Rails, and which configuration does a Rails CRUD for people never write because of them?

level: juniorimportance: must knowfreq 70%

answer

  1. the Rails way as default
  2. no table or column mapping
  3. implicit render of the matching template
  4. form_with infers URL, method, param key
  5. one authoritative place per fact

basics

~20 s

In Rails, convention over configuration means names and locations imply behaviour, and DRY means each fact lives in one place; a people CRUD declares no table mapping, columns, routes per action, template paths or form URLs, because Rails derives them.

solid answer

~50 s

Convention over configuration means Rails assumes sensible defaults (names, file locations, columns) and only asks you to configure the exceptions. DRY, "every piece of knowledge must have a single, unambiguous, authoritative representation", means each fact is stated once. For a people CRUD that removes a lot: `Person` maps to `people` and reads its attributes from the table's columns, so nothing is declared twice; `created_at` and `updated_at` are filled automatically; `resources :people` defines the routes for the seven standard actions; an action that does not call `render` renders `app/views/people/<action>.html.erb`; and `form_with model: @person` infers the URL, the HTTP method and the `person` param key. Shared markup goes into partials, shared controller setup into `before_action`. The cost is that conventions must be learned, and code that fights them, such as hand-named controllers or legacy tables, needs explicit configuration.

code

ruby · 14 lines
ruby
# config/routes.rb
resources :people

# app/controllers/people_controller.rb
class PeopleController < ApplicationController
  def index
    @people = Person.order(:name)
    # no render call: Rails renders app/views/people/index.html.erb
  end

  def show
    @person = Person.find(params[:id])
  end
end

go deeper

for a junior

Define both principles in a sentence each and give concrete examples: table mapping, implicit render, resources routes, form_with inference.

for a middle

Explain the mechanisms behind the examples and where configuration takes over: legacy tables, non-standard controllers, words the inflector gets wrong.

for a senior

Weigh the trade: shared knowledge and less code against surprise for newcomers, and recognise when DRY abstractions couple things that should change separately.

for a principal

Decide where a codebase may deviate from conventions and how those deviations are documented, so the shared knowledge stays reliable.

## The two principles The Rails guides name two guiding principles: - **Don't Repeat Yourself (DRY)**: "Every piece of knowledge must have a single, unambiguous, authoritative representation within a system." - **Convention over configuration**: Rails "has opinions about the best way to do many things in a web application, and defaults to this set of conventions, rather than require that you define them yourself through endless configuration files." They reinforce each other. A convention is a fact the whole team, and every Rails component, already knows, so nobody has to repeat it in configuration. ## What a people CRUD does not write | Concern | What Rails derives | What you would write without the convention | |---|---|---| | Table mapping | `Person` uses the `people` table | a table name per model | | Attributes | read from the table's columns at runtime | a field list duplicating the schema | | Timestamps | `created_at`, `updated_at` set on create and update | callbacks setting them | | Routes | `resources :people` gives index, show, new, create, edit, update, destroy | a route declaration per action | | Templates | an action without `render` renders `people/<action>` | a template path per action | | Forms | `form_with model: @person` picks URL, POST or PATCH, and `person[...]` field names | the URL, method and field prefix by hand | | Foreign keys | `belongs_to :person` uses `person_id` | a key name per association | Each row is a decision taken once, for every Rails app, so it is never discussed per project. ## DRY in the same app - **The schema is the source of attributes**: Active Record models do not list their columns; adding a column in a migration makes the attribute appear. - **Validations once, used everywhere**: a `validates :name, presence: true` on the model is what every form, API call and console session goes through, and its messages feed the form's error display. - **Partials** such as `people/_form.html.erb` are shared by `new` and `edit`. - **`before_action :set_person`** loads the record once for `show`, `edit`, `update` and `destroy`. ## Where it stops being free 1. **The conventions must be learned.** A newcomer sees `render` missing from an action and wonders where the HTML comes from. Interviews probe exactly this. 2. **Exceptions need configuration.** A legacy table called `tbl_person`, a controller deliberately named differently, or a word the inflector gets wrong all require explicit settings, and those settings are the places future readers will be surprised. 3. **DRY can be overdone.** Extracting two similar-looking pieces of code into one shared abstraction couples things that may change for different reasons. DRY is about one source of truth for a *fact*, not about removing every repeated line. ## The comparison interviewers sometimes ask for In frameworks that map objects to tables and URLs to handlers through explicit declarations, such as annotations on every entity and route, the same information exists, written out for each class. That is easier to read in isolation and harder to keep consistent. Rails chooses the opposite trade: less code, more shared knowledge. Neither is free; what matters in an interview is naming what each side pays. ## How to talk about it A strong answer gives one concrete example per principle, such as the implicit render and the schema-derived attributes, and one example of when the convention is overridden, which shows you know its edges.

  • What does a Rails controller action render when it never calls `render` or `redirect_to`?
    It renders the template matching the controller and action, such as `app/views/people/index.html.erb` for `PeopleController#index`, inside the controller's layout. If no template exists, a browser request raises a missing-template error, while other request formats get an empty `204 No Content` response.
  • Give an example where applying DRY too eagerly hurts a Rails app.
    Sharing one partial between an admin and a public profile page because they look alike today. When one page needs a field the other must hide, the partial grows flags and conditionals. Two templates that repeat some markup but change independently are easier to maintain than one that serves two masters.

  • Convention over configuration means Rails apps cannot be configured.
  • Active Record models must list their columns as attributes.
  • Every controller action has to call render explicitly.
  • DRY means no two lines of code may ever look alike.
  • form_with always needs an explicit url: and method: option.
open as a page

In a Rails genealogy app, which names does a Person model imply for its table, files, controller, routes, views and foreign keys?

level: juniorimportance: must knowfreq 80%

basics

~10 s

A Rails Person model lives in app/models/person.rb and maps to the people table; PeopleController handles resources :people, with helpers like people_path and person_path, templates under app/views/people, and other tables point at it through person_id.

open as a page

In a Rails app, what does `inflect.acronym "HTTP"` change for a file named lib/archive_http_client.rb, and what breaks if you add it late?

level: middleimportance: should knowfreq 35%

basics

~10 s

With inflect.acronym "HTTP", Rails camelizes archive_http_client to ArchiveHTTPClient instead of ArchiveHttpClient, so the file must define that constant; adding it after code uses ArchiveHttpClient breaks loading until every reference is renamed.

open as a page

In Rails, what does "fat model, skinny controller" mean, and when should a growing Person model be split up?

level: middleimportance: should knowfreq 50%

basics

~20 s

In Rails, "fat model, skinny controller" means domain rules live on models while controllers only handle params, loading records, calling one model method and rendering; when a Person model grows into a god object, extract plain classes under app/models.

open as a page

In a Rails genealogy app, what goes wrong when a Census model relies on the default English inflections, and how does inflect.irregular fix it?

level: middleimportance: should knowfreq 40%

basics

~20 s

Rails' default rules leave census unchanged when pluralizing and singularize censuses to censuse, so a Census model gets a census table, census_index route helpers and a broken has_many :censuses; inflect.irregular "census", "censuses" fixes every derived name at once.

open as a page

In the Rails Doctrine, what do the menu is omakase, provide sharp knives and the majestic monolith mean, and where do they show in Rails 8.1?

level: seniorimportance: should knowfreq 30%

basics

~20 s

The Rails Doctrine's omakase pillar means Rails picks a full default stack; sharp knives means it hands developers powerful tools and trusts them; the majestic monolith means one integrated app. Rails 8.1 shows all three in its generator defaults and APIs.

open as a page