skip to content

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

level: middleimportance: should knowfreq 50%

answer

  1. controllers translate HTTP, models decide
  2. domain rules on the model
  3. one model call per action
  4. god object warning signs
  5. plain classes under app/models

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.

solid answer

~40 s

The maxim puts business logic where it can be reused and tested without HTTP: on the model. A skinny `MergesController#create` finds two `Person` records, calls `person.absorb!(duplicate)` and redirects; the rules for merging, the transaction and the moving of events live in `Person`. That keeps controllers uniform and lets jobs, scripts and the console reuse the same behaviour. The maxim has a failure mode: a `Person` with hundreds of methods, many callbacks and knowledge of mailers and external services becomes a god object. Signs include methods that do not use the record's own state, callbacks with side effects such as sending email, and unrelated groups of methods. Split by extracting plain Ruby classes such as `Person::Merger` in `app/models/person/merger.rb`, or concerns for shared behaviour, while keeping the controller just as thin.

go deeper

for a junior

Recall the split: controllers handle the request and response, models hold the business rules, and show one skinny action calling one model method.

for a middle

Explain why logic on the model is reusable from jobs, scripts and tests, and show an extraction into a plain class without touching callers.

for a senior

Recognise god-object signs, especially side-effecting callbacks, and choose between plain classes, concerns, role folders and jobs for each case.

for a principal

Set the team's rules for where domain logic lives as the app grows, so models stay cohesive without scattering logic across vague service layers.

## What the maxim says **"Fat model, skinny controller"** is long-standing Rails advice about where code goes: - **Controllers** translate HTTP into calls: read params, load records, authorize, call the domain, choose a response. - **Models** hold the domain: validations, associations, scopes and methods that express what the business does. The advice follows from Rails' MVC split and from DRY: a rule stated on the model is used by every controller, job, script and console session, while a rule written in a controller is available only to that action. ## A skinny controller In a genealogy app, merging a duplicate person record is a domain operation: ```ruby class MergesController < ApplicationController def create person = Person.find(params[:person_id]) duplicate = Person.find(params[:duplicate_id]) person.absorb!(duplicate) redirect_to person, notice: "Records merged." end end ``` The controller does not know how a merge works. The model does: ```ruby class Person < ApplicationRecord has_many :events, dependent: :destroy def absorb!(duplicate) transaction do duplicate.events.update_all(person_id: id) duplicate.destroy! end end end ``` The same `absorb!` can be called from a background job that merges suggested duplicates, from a script, or from a test without building a request. ## When "fat" becomes a problem Models attract code because they are the obvious home. Warning signs that a model has become a **god object**: 1. **Size and spread**: hundreds of lines, with groups of methods that never call each other. 2. **Side effects in callbacks**: `after_create` sending email or calling an external service, so creating a record in a test or a script triggers them too. 3. **Knowledge of other layers**: the model builds view strings, reads `params`-shaped hashes or knows about HTTP clients. 4. **Methods that ignore the record**: class methods that orchestrate several models are operations, not behaviour of one record. ## How to slim a model without fattening the controller | Technique | Where it lives | Fits | |---|---|---| | A plain Ruby class | `app/models/person/merger.rb` defining `Person::Merger` | one operation with its own steps and state | | A concern | `app/models/concerns/` | behaviour shared by several models | | A role folder | `app/services`, `app/queries`, `app/forms` | a family of similar objects | | A job | `app/jobs` | slow or external work triggered by the model | The aim is that each class has one reason to change. `Person#absorb!` can stay as a one-line method that delegates to `Person::Merger.new(self, duplicate).call`, so callers keep the readable API. ## What the maxim is not - It is **not** "put everything in Active Record classes": `app/models` may hold plain Ruby objects, and many teams keep domain classes there regardless of whether they have a table. - It is **not** an argument against thin layers elsewhere: a form object or a query object is still a model in the MVC sense. - It does **not** forbid logic in controllers: authorization checks, choosing a response format and handling a failed save are controller work. ## What interviewers listen for A candidate who says "keep controllers thin" and stops there gives half an answer. The full answer names what belongs in each layer, shows a concrete extraction, and admits the failure mode on the model side.

  • Why can an `after_create` callback that sends a welcome email make a Person model hard to work with?
    Every creation path triggers it: seeds, imports, tests, console sessions and merges. Scripts that create thousands of records send thousands of emails unless someone remembers to skip the callback. Triggering the email from the action or operation that represents sign-up keeps the side effect where it is intended.
  • Where would you put `Person::Merger`, and how does the controller change?
    In `app/models/person/merger.rb`, defining `Person::Merger` next to the model it serves. The controller does not change: it still calls `person.absorb!(duplicate)`, and that method delegates to the new class. Extracting behind the model's method keeps every caller untouched.

A skinny controller is a restaurant waiter: it takes the order, passes it to the kitchen and brings back the plate. The recipes live in the kitchen (the model), and when one kitchen grows too big, you add stations, not more duties for the waiter.

  • Skinny controllers mean controllers should contain no code at all.
  • Every model method must be backed by a database column.
  • Business rules belong in controllers so each action can differ.
  • Moving logic out of a model always means moving it into the controller.
  • Plain Ruby classes are not allowed in app/models.