Skip to content

project rulesBackend and APIsrubyrails

Rails Rules

Project rules for Rails apps: lean controllers, plain Ruby service objects, safe Active Record queries, reversible migrations and RSpec request specs.

npx workspacetemplates add rails-rules --tool claude-code

Writes 1 file to your project root

CLAUDE.mdadd to your file26 lines · 1.4 KBFile
# Project rules: Rails

## Structure
- Follow Rails naming and RESTful routes; add a custom action only after trying to model it as another resource.
- Controllers: load, authorize, call, respond. Multi-step business logic goes into a plain Ruby object in `app/services` with a single public `call` method that returns a result, not a half-saved record.
- Avoid callbacks for anything but data normalization. Never send email or call an API from a model callback.

## Params and security
- Use strong parameters with an explicit `permit` list. Authorize with Pundit or CanCanCan on every action. Credentials live in `rails credentials` or the environment.

## Active Record
- Prevent N+1 with `includes`, `preload` or `eager_load`; add the Bullet gem in development. Use `pluck`, `exists?` and `find_each` where a full model is wasteful.
- Express rules as validations plus matching database constraints. Put named `scope`s on models for reuse.
- Wrap related writes in `transaction`.

## Migrations
- Reversible with `change` or explicit `up` and `down`. Add indexes on foreign keys, and use `strong_migrations` patterns for large tables. Commit `db/schema.rb`.

## Jobs
- Slow work goes to Active Job; pass ids, not objects.

## Tests
- RSpec request specs and model specs with FactoryBot. Avoid testing private methods.

## Before finishing
Run `bundle exec rubocop`, `bin/rails db:migrate:status` and `bundle exec rspec`.

What it does

Rails conventions are strong, but an agent can still write business logic in callbacks or loop over associations one record at a time. These rules keep the work in the right layer.

They also treat the database as a real boundary: constraints, indexes and migrations get the same care as application code.

Example

you › Let admins deactivate a user and notify them

agent › The agent adds a service object that updates the user in a transaction and enqueues a mailer job, authorizes through the policy, and writes a request spec covering the forbidden case.

Questions

Does it cover Rails API mode?
Yes for structure, queries and tests. Replace view and form guidance with your serializer library choice if you use one.
Why pass ids to jobs?
Objects can be stale or fail to serialize by the time the job runs. Loading by id gives the job fresh data.
  • ASP.NET Core Web API Rules

    Project rulesdotnet

    Free
  • Django Rules

    Project rulespython

    Free
  • Flask Rules

    Project rulespython

    Free
  • Go Service Rules

    Project rulesgo

    Free