skip to content

In a Rails codebase, when is a model concern a genuine role, when is it a junk drawer, and when should the code become a service object instead?

level: seniorimportance: should knowfreq 45%

answer

  1. a role is an adjective: SoftDeletable
  2. same behaviour across several models
  3. junk drawer: methods moved to shrink a file
  4. hidden callbacks across files
  5. a process across models is a verb

basics

~20 s

A concern is a role when it names one cohesive trait several models share, like SoftDeletable. It is a junk drawer when it only moves unrelated methods out of a fat model. A multi-step process touching several models belongs in a plain object.

solid answer

~50 s

A good concern names a **role** a model plays, usually an adjective such as `SoftDeletable`, `Searchable` or `Taggable`, and bundles everything that role needs: its scopes, callbacks, columns contract and methods, readable in one file and reusable across hosts. A **junk drawer** is a module like `DocumentHelpers` or `DocumentMethods` created to make `document.rb` shorter: the code is still part of the same class, it reaches into the host's private state and other concerns, and callbacks now hide in files nobody opens. When the code is a **process** rather than a trait, say archiving a folder by trashing its documents, removing them from search and emailing the owner, a plain Ruby object named for the operation (`Folder::Archiver` or `ArchiveFolder`) with one public method is clearer, explicit about order, and easier to test. A concern used by a single model can still be a fine way to group one coherent aspect, if it is cohesive.

go deeper

for a junior

Recall that a concern should name one shared trait, such as SoftDeletable, rather than collect unrelated methods.

for a middle

Explain the signs of a junk drawer, meaningless names, unrelated methods, host-specific coupling, and why include order affects concern callbacks.

for a senior

Choose between a role concern, the model, and a plain object for a workflow, and justify it with cohesion, contracts and explicit ordering.

for a principal

Set the codebase's conventions for concerns and service objects, and how reviews enforce them, so the choice does not depend on who wrote the code.

## Three shapes of shared code Once a Rails model grows, three tools compete for its code: a **concern** mixed into the model, the **model itself**, and a **plain Ruby object** (often called a service object, though Rails has no built-in class or generator for one). Interviewers ask when each fits because getting it wrong produces the two most common Rails code smells: fat models and concerns that only hide the fat. ## A concern as a role A concern works best when it names a role, usually an adjective: the model *is* soft-deletable, searchable, taggable, publishable. Signs of a healthy role: - **Cohesion.** Its scopes, callbacks, validations and methods all serve one idea; removing one makes the others pointless. - **A clear contract.** It states what it needs from the host, for example a `deleted_at` column, and uses only the host's public API. - **Readable alone.** You can understand `SoftDeletable` without opening `Document`. - **Often reused**, across `Document`, `Folder` and `Note`, though a single-model concern that groups one coherent aspect, such as `Document::Versioning`, can be fine too. ## A concern as a junk drawer The failure mode is a module created to make a file shorter. Typical signs: 1. A name with no meaning: `DocumentHelpers`, `DocumentExtras`, `Shared`. 2. Unrelated methods side by side: a PDF export next to a permission check next to a slug generator. 3. Reaching into the host's private methods or instance variables, or calling another concern's helpers, so the concern only works in one model. 4. Callbacks spread across several concerns, whose combined order depends on which `include` line comes first in the model. Nothing about the object changed: `Document` still has all those methods and responsibilities. The code is merely spread across files, which makes it harder to find while looking tidier. The test is simple: if the concern could not be included into a second model without edits, it is probably not a role. ## When a plain object fits better Some code is a **process**, a verb rather than an adjective: archive a folder, merge two documents, import a batch. Such code usually: - touches several models in a set order; - has side effects beyond the database, such as enqueuing jobs or sending mail; - needs inputs that are not attributes of any one record, such as the acting user. Putting that in a concern spreads one workflow over callbacks that fire on every save. A plain object makes the order explicit: ```ruby # app/models/folder/archiver.rb class Folder::Archiver def initialize(folder, by:) @folder, @user = folder, by end def call Folder.transaction do @folder.documents.kept.find_each(&:soft_delete) @folder.soft_delete end ArchiveNotificationJob.perform_later(@folder, @user) end end ``` ## Comparing the options | Question | Concern (role) | Plain object (process) | |---|---|---| | Grammar | adjective: `SoftDeletable` | verb or noun of an action: `Folder::Archiver` | | Scope | one trait of one record type | a workflow across records | | Triggered by | the host's lifecycle and calls | an explicit `call` from a controller or job | | Order of side effects | implicit, via callbacks | explicit, line by line | | Testing | shared tests per host | unit test of one object | Both styles appear in well-run Rails codebases; the debate is about where each belongs, not about banning one. ## A note on naming Names are the cheapest signal. Role concerns read as adjectives that complete "a document is ...": soft-deletable, searchable, publishable. Process objects read as actions: `Folder::Archiver`, `ImportBatch`. A concern whose name completes neither sentence is worth a second look. ## How to answer in an interview A strong answer names criteria rather than a rule: cohesion, a stated contract, reuse, and whether the code is a trait or a process. It also notes the cost of each: concerns add implicit behaviour to every host, while service objects add indirection and can drain models of the domain logic that belongs there. Refactoring a junk drawer usually means splitting it, moving the true trait into a named concern, the workflow into an object, and leaving model-specific logic in the model.

  • A pull request moves forty methods from `document.rb` into `DocumentMethods` and `DocumentCallbacks` concerns. What review feedback would you give?
    Ask what role each module names. If neither can be included into another model, the split only hides size. Suggest grouping by aspect instead, such as a cohesive `Document::Versioning` concern, moving any multi-model workflow into a plain object, and keeping callbacks next to the behaviour they serve rather than in a separate callbacks file.
  • Why are callbacks inside several concerns a particular risk?
    Each concern's `included` block adds callbacks to the host when its `include` line runs, so the final callback order follows the order of `include` lines in the model. Reordering includes can change behaviour, and a reader of the model sees no callbacks at all. Keep side-effecting workflows explicit, or document the ordering the concerns rely on.

  • Moving a fat model's methods into concerns makes the model's responsibilities smaller.
  • Any module in app/models/concerns is a good concern because Rails generated the folder.
  • Rails ships a service-object base class and a generator for it.
  • A concern should reach into the host's private methods to avoid duplicating code.
  • Callbacks spread across concerns run in a fixed order regardless of include order.