Lesson 1 of 9 · 24m
Convention Leverage and the Majestic Monolith
Treat Rails conventions as leverage, not constraint, and organize a monolith so teams can move quickly without dissolving boundaries.
The teams that complain Rails does not scale usually scaled chaos first. Rails scales when you exploit its conventions: predictable names, predictable lifecycles, predictable places for behavior. Convention is not aesthetic purity. It is compression. A new hire should open app/models/user.rb, config/routes.rb, and db/schema.rb and already know where most of the application lives.
The monolith is a deployment shape, not a design excuse
A monolith can still have internal boundaries. What it cannot survive is every object reaching into every other object because Ruby allows it. Organize around business capabilities:
app/
controllers/billing/invoices_controller.rb
models/billing/invoice.rb
jobs/billing/invoice_finalize_job.rb
policies/billing/invoice_policy.rbIf Billing::Invoice is reaching into inventory internals, or controllers are stitching six models together with ad hoc transactions, the problem is architectural ambiguity, not Rails.
Controllers coordinate, models own invariants
A controller should authenticate, authorize, parse input, invoke behavior, and choose the response. It should not contain the business policy:
class Billing::InvoicesController < ApplicationController
def finalize
invoice = Current.account.invoices.find(params[:id])
authorize invoice, :finalize?
Billing::FinalizeInvoice.call(invoice, actor: Current.user)
redirect_to billing_invoice_path(invoice), notice: "Invoice finalized"
rescue Billing::InvoiceNotFinalizable => e
redirect_to billing_invoice_path(invoice), alert: e.message
end
endThe service object is not mandatory. A model method can own the workflow when it is small. The rule is simpler: business invariants need one obvious owner.
Use Rails defaults until you can name the cost
Rails gives you migrations, validations, CSRF protection, parameter filtering, encrypted credentials, structured logging, and a coherent asset pipeline. Replacing them because a blog post said so creates operational debt. Deviate only when you can explain the cost in production terms: queue latency, cache hit ratio, query count, memory ceiling, deploy safety.
Beware the hidden framework you built
The worst Rails applications are full of clever base classes, magic callbacks, global helpers, and monkey patches that require tribal knowledge. If a developer needs a conference talk to understand why save sends email, rebuild the path. Explicit beats clever when the pager goes off.
# Prefer explicit orchestration:
Billing::FinalizeInvoice.call(invoice, actor: user)
# Over six callbacks firing because a status column changed.Field notes
concernsare mixins, not architecture. Use them for reusable behavior, not to hide unrelated model mass.- Namespacing a constant does not create a boundary if every object still shares one giant transaction graph.
- Rails generators are documentation with side effects; use them so files land where the next developer expects.
- A monolith with clear modules is easier to split later than a premature constellation of services sharing one database.
Was this lesson helpful?