rails-active-record
Rails ActiveRecord Conventions
Models persist and validate data. Business logic, cross-model side effects, and query optimization live elsewhere.
No Business Logic in Models
Models hold persistence, validations, associations, and simple derived attributes. Anything else — external API calls, sending emails, multi-model orchestration — belongs in a service object.
| Wrong | Right |
|---|---|
user.register! sends welcome email internally |
RegisterUser.call sends the email, model just saves |
Order#charge_card calls the payment gateway |
ChargeOrder.call calls the gateway |
Rule of thumb: if a model method's implementation needs to know about a different domain concept (payments, mailers, external APIs), it doesn't belong on the model.
Non-Database-Backed Models Are Fine
Not every model needs a table. When data has validations, attributes, and a natural name but no persistence (a multi-step form, an API request payload, a search filter set), model it with ActiveModel::Model / ActiveModel::Attributes instead of bolting attributes onto a controller or stretching an AR model to cover a shape it doesn't persist.