Rails Best Practices Core
Use this as the default baseline for Rails work. It started from 37signals' codebases (Campfire, Fizzy) and DHH's reviews; the parts that are a matter of taste live in 37signals-style. When the app follows that style (no service objects, CRUD-only routing, records for state), apply both.
Core Defaults
- Prefer clear, explicit code over clever abstractions. Abstractions must earn their keep; if you can't point to 3+ variations that need it, inline it.
- Keep controllers thin and put domain behavior in models.
- Prefer Rails conventions and built-ins before adding gems.
- Model state and behavior with domain concepts, not ad-hoc flags.
- Scope tenant/user data through ownership boundaries.
- Favor database constraints for hard invariants; only validate in AR when you need user-facing error messages.
- Keep interfaces small; don't add public methods that aren't used anywhere.
- Prefer write-time computation over expensive read-time composition (counter caches, delegated types, precomputed roll-ups,
dependent: :delete_allwhen no callbacks needed). - Use
params.expect(...)for strong params in modern Rails. - Let it crash: bang methods (
create!), handle exceptions at boundaries. Only use!when a non-bang counterpart exists. - Fix root causes, not symptoms (e.g.
enqueue_after_transaction_commitover retry logic for races). - Ship tests in the same PR as behavior changes.
Modeling
- Callbacks for setup/cleanup, not business logic. Keep callback counts low.
- Rails shortcuts to reach for:
normalizes(data cleanup before validation),store_accessor(JSON columns),delegated_type(heterogeneous collections),generates_token_for(expiring signed tokens), string enums viaenum :status, %w[drafted published].index_by(&:itself),after_save_commit,touch: truechains for cache invalidation,delegate. - Bulk writes with
insert_all/upsert_all;dependent: :delete_allon join tables with no callbacks.
Naming
- Spend time on names: naming is design.
ClosurebeatsCardClose;MentionbeatsUserReference. - Positive names:
activenotnot_deleted,visiblenotnot_hidden. - Semantic associations named for role:
belongs_to :creator, class_name: "User"notbelongs_to :user. - Domain-driven over technical:
quota.depleted?notquota.over_limit?. - Business-focused scopes:
:active,:unassigned,:golden, not SQL-ish:without_pop. - Consistent domain language: don't mix
source/resource/containerfor one concept.
Routing
- Prefer resourceful routes. Before adding a custom action, check whether the state change reads better as a nested resource (
resource :archive). resolve "Comment"for polymorphic URL generation to the parent with an anchor.
Authorization
- Keep permission rules in one place (model predicates or policy objects), not scattered conditionals across controllers.
- Controllers fail closed (
head :forbidden) when access can't be proven.
Dependencies
Before adding a gem ask: can vanilla Rails do this? Is a small amount of in-repo code simpler than a dependency you'll have to upgrade?
Review Priorities
- Correctness and data safety.
- Multi-tenant/security boundaries.
- Maintainability and readability.
- Performance hot spots.
- Style and polish.
Always Flag
- Unscoped record lookups in tenant-aware flows (
Comment.find(params[:id])). - New dependencies without strong justification.
- In-memory filtering/sorting that belongs in SQL (and
.map(&:name)where.pluck(:name)works). - Pages with forms using HTTP caching (
fresh_when/etag): stale CSRF tokens cause 422s. - String status checks (
status == "x") when predicate-style APIs are available (StringInquirer / string enums). validates :x, uniqueness: truewithout a backing unique index.- Helpers depending on implicit instance variables instead of explicit arguments.
- Unescaped interpolation into
html_safestrings. Escape first:"<b>#{h(input)}</b>".html_safe. - Metaprogramming for 2-3 cases: just write the methods.
Review Output
- Start with highest-severity findings.
- For each finding: issue, impact, concrete fix with file:line references.
- Be direct and practical; "This is over-engineered" is a complete sentence.
- End with either
Ship itor a short prioritized fix list.