Where code belongs.
THE PROBLEM
Logic accumulating in controllers and models until neither can be understood.
WHAT TO SEPARATE
Handling the request Performing the work Accessing data Producing output
WHAT TO CREATE
Dedicated classes for units of work.
WHAT TO NAME THEM AFTER
What they do.
WHAT THAT BUYS YOU
Logic that can be called from a controller, a command, a queued job or a test.
WHY THAT MATTERS
The same operation is frequently needed from several places.
WHAT TO PUT IN A MODEL
Behaviour belonging to the record itself Relationships Reusable query scopes
WHAT NOT TO PUT THERE
Operations coordinating several models Anything involving external services
WHAT TO CONSIDER FOR COMPLEX APPLICATIONS
Separating query logic from command logic Dedicated classes for external service access
WHAT NOT TO DO
Adopt elaborate architecture for a small application.
WHAT TO AIM FOR
Each piece having one clear responsibility and one obvious place.