activerecord-patterns
ActiveRecord Patterns
Senior Rails ActiveRecord conventions, organized as before/after pattern pairs. AI agents routinely violate these — they reach for callbacks where a job belongs, use
where.firstwhenfind_byexists, default-scope themselves into corners, and lose database integrity through naïve polymorphic associations. This skill makes the agent write models the way the Rails core team writes them.
The opinion (DHH-leaning default)
Fat models are fine. Put domain logic in the model. Use ActiveSupport::Concern to split fat models into focused mixins. Reach for service objects only when (a) orchestrating multiple models in a single workflow, (b) calling external APIs, or (c) the workflow lives outside an obvious model's domain.
DHH on this:
"I far prefer
current_account.posts.visible_to(current_user)to involving a third query object."
— Put Chubby Models on a Diet with Concerns
The counter-position: small-object-per-responsibility (the Sandi Metz / 7±2 doctrine) keeps churn cheaper in very large codebases. We acknowledge that and recommend it only at team-size-large (20+) — see service-objects-vs-fat-models for the full breakdown.