# Rails equivalents

Source: https://bogie-go.com/docs/from-rails.html

A lookup table for anyone who knows Rails and is reading or extending a Bogie app: the Rails name for something, and where it lives here. It is reference only; the other guides cover how Bogie works on its own terms.

The one idea to carry over: **Go has no autoload and no `method_missing`.** Nothing exists until it is written down and registered, so every "magic" Rails step has a visible line here, and the generators write those lines so you do not have to find them.

## Commands

| Rails | Bogie | Notes |
| --- | --- | --- |
| `rails new blog` | `bogie new blog` | `--jobs` adds River; `--id=uuid` swaps the default bigint ids for uuids. API-only, Postgres-only; no `--api`, no `-d`. |
| `rails server`, `rails s` | `bogie server`, `bogie s` | Migrates at boot, as a deployed container does. Reloads on save in development. |
| `bin/dev`, Spring | none; built into `bogie server` and `bogie worker` | Save a file and Air rebuilds and restarts it; a new migration is picked up on the next boot too. Production runs the compiled binary, no watcher. |
| `rails g scaffold post title:string` | `bogie g scaffold post title:string` | Model, views and a wired controller; no HTML. |
| `rails g model post title:string` | `bogie g model post title:string` | Migration, queries, model, store, specs and a factory; runs sqlc. |
| `rails g controller posts index show` | `bogie g controller posts index show` | Writes it *and* registers it. `new` and `edit` are refused. |
| `rails g controller admin/reports` | `bogie g controller admin/reports` | A namespace is a package, `app/controllers/admin_controllers/`. |
| `rails g migration add_slug_to_posts slug:string` | `bogie g migration add_slug_to_posts slug:string` | SQL, not Ruby; `create_`, `add_x_to_`, `remove_x_from_` get their SQL written. |
| `rails routes` | `bogie routes` | Method, path and handler for every mounted route, read from the booted app. Development only. |
| (`db/seeds.rb`, by hand) | `bogie g seed demo_posts` | `db/seeds/demo_posts.go`, registered above `// bogie:seeds`. See [Seeds](https://bogie-go.com/docs/seeds.html). |
| `rails g job send_welcome` | `bogie g job send_welcome` | A River worker, registered; the first one on an app with no jobs adds them first. |
| `rails g authentication` | `bogie g authentication secret`, `token` or `api_key` | A middleware on a route group; the shape says who the caller is. See [Authentication](https://bogie-go.com/docs/authentication.html). |
| (a service object, by hand) | `bogie g service publish_post` | A package with a `Run(ctx)`, not wired anywhere. |
| `rails destroy ...`, `rails d` | `bogie destroy ...`, `bogie d` | Removes the registration lines too. |
| `rails db:create` … `db:rollback` | the same words | `db:prepare`, `db:reset`, `db:seed`, `db:migrate:status`, `db:migrate:redo`, `db:version`. |
| `rails credentials:edit` | `bogie credentials:edit` | `-e production` for that environment's file and key. |
| `rails test` | `bogie test`, `bogie t` | `go test -p 1 -race -cover ./...`; Ginkgo specs, and they need Postgres up. |
| `bin/ci` | `bin/ci` | The whole pipeline, locally; green signs off with `gh signoff`. |
| `rails console` | none | No REPL. `make psql` opens the database; a throwaway `_test.go` is the Go way. |
| `rails dbconsole` | `make psql` |  |
| `bundle install` | `go mod download` | `go mod tidy` is `bundle` after a Gemfile edit. |
| `rubocop` | `bogie lint` | gofmt, go vet, golangci-lint; `.golangci.yml` is the config. |
| `kamal deploy` | `kamal deploy` | Same tool, same `config/deploy.yml`. |

There is also `bogie doctor`, with no Rails equivalent: it checks that the markers the generators insert above are intact and that every controller is both constructed and mounted.

## Where things live

| Rails | Bogie | Notes |
| --- | --- | --- |
| `config/routes.rb` | `app/controllers/routes.go` | The table; each controller's `SetupRoutes` adds its own routes under a group. |
| `app/controllers/posts_controller.rb` | `app/controllers/posts_controller.go` | A `PostsController` type: its dependencies, routes and actions. |
| `app/controllers/admin/reports_controller.rb` | `app/controllers/admin_controllers/reports_controller.go` | The package holds a `Server` with one field per controller and a `routes.go` for the group's middleware. |
| `app/models/post.rb` | `app/models/post.go` + `db/adapter/posts.go` + `db/queries/posts.sql` | Split in three: the type and its validations; the store methods; the SQL. See below. |
| `app/views/posts/*.json.jbuilder` | `app/views/posts.go` | Request and response structs. A model type never doubles as a wire type. |
| `app/jobs/send_welcome_job.rb` | `app/jobs/send_welcome.go` | An `Args` struct and a `Worker`, registered in `app/jobs/jobs.go`. |
| `app/services/` | `app/services/<name>/` | One package per service, `ctx` first, knows nothing about HTTP. |
| `app/helpers/`, `lib/utils.rb` | none | Rule 6: no `util`, `helpers` or `common`. A package is named for what it provides. |
| `app/controllers/concerns/` | none | Compose with plain functions and small interfaces. |
| `db/migrate/*.rb` | `db/migrate/*.sql` | goose SQL with `-- +goose Up` and `Down`, embedded in the binary. |
| `db/schema.rb` | none | The migrations are the schema; sqlc compiles the queries against them. |
| `db/seeds.rb` | `db/seeds/*.go` | One function per file, registered above `// bogie:seeds`; `db seed` re-runs every one, so each must be safe to run twice, as `db/seeds.rb` already has to be. |
| `public/404.html`, `500.html`, `robots.txt` | `public/`, go:embedded | Served below the router for any request no route answered, API mode included. See [Static files and error pages](https://bogie-go.com/docs/static-files.html). |
| `config/database.yml` | `DATABASE_URL` in credentials or the environment | One URL per environment; test never reads a credentials file. |
| `config/credentials.yml.enc`, `master.key` | the same files | Same pattern, same names, same `-e production`. |
| `config/application.rb`, initializers | `app/application.go` | Where everything is constructed and wired, in order, with the markers. |
| `config/puma.rb` | none | `net/http` with timeouts set in `app/controllers/application.go`. |
| `config/deploy.yml` | `config/deploy.yml` | Kamal 2, unchanged. |
| `Gemfile`, `Gemfile.lock` | `go.mod`, `go.sum` | Tools (sqlc, goose) are pinned there too, under `tool`. |
| `.rubocop.yml` | `.golangci.yml` |  |
| `test/`, `spec/` | beside the code, `*_test.go`; shared helpers in `test/` | Ginkgo and Gomega (`Describe`/`Context`/`It`, `Expect`). `test/testdb`, `test/testapp`, `test/factories` (factory_bot, by hand), request specs. No fixtures: seeds. |
| `Procfile.dev`, `bin/dev` | `make server`, `make worker` |  |
| `config/spring.rb` | `.air.toml` | Settings for the reload `bogie server` and `bogie worker` run under in development; delete it to turn reload off. |
| `AGENTS.md`, `CLAUDE.md` | the same | The rules, the commands, a recipe per task. |

## A model, in three places

Rails's `Post` is one class that is a table, a type and a query builder. Here that is three files, each with one job:

- **`app/models/post.go`** is the type and its `Validate`. It imports nothing third-party (rule 3). Ids are `int64` under the default `bigint` scheme, as in Rails, or strings in an app made with `--id=uuid`. See [Id schemes](https://bogie-go.com/docs/models.html#ids).
- **`db/queries/posts.sql`** is the SQL, by name: `GetPost`, `ListPosts`, `CreatePost`. sqlc compiles it into `db/sqlc/posts.sql.go`, and a query that names a column that does not exist fails at `make sqlc`, not in production.
- **`db/adapter/posts.go`** is the store: `CreatePost`, `GetPost` and so on, converting between the model and sqlc's row type, mapping "no rows" to `models.ErrNotFound`, and running `Validate` before create and update.

So `Post.find(id)` is `store.GetPost(ctx, id)`, `Post.create(attrs)` is `store.CreatePost(ctx, models.Post{...})`, and a new query is a new named statement in the `.sql` file plus a store method that calls it. There is no `where` builder; the SQL is written out, which is the point.

Associations are a foreign key and a query. `post:references` on a model writes the column, the constraint and the index; `has_many :comments` is `ListCommentsForPost` in `db/queries/comments.sql` when you need it.

## In the code

| Rails | Bogie |
| --- | --- |
| `params[:id]` | `c.Param("id")` |
| `params.require(:post).permit(:title)` | `var req views.PostRequest; c.ShouldBindJSON(&req)`; only the struct's fields come through |
| `render json: post` | `c.JSON(http.StatusOK, postView(post))` |
| `head :no_content` | `c.Status(http.StatusNoContent)` |
| `validates :title, presence: true` | `func (p Post) Validate() error { ... return fmt.Errorf("%w: title is required", ErrInvalid) }` in `app/models`; runs on save |
| `rescue_from ActiveRecord::RecordNotFound` | `ctl.fail(c, err)`: `ErrNotFound` is 404, `ErrInvalid` is 422, anything else is a logged 500 |
| `before_action :authenticate` | middleware on the group, in `routes.go`, so the exposure boundary is visible in one place; `bogie g authentication` writes it and a `WithUser` group helper. See [Authentication](https://bogie-go.com/docs/authentication.html). |
| `Current.user` | `middlewares.Subject(c)`, the verified token's subject; load the record if you need it |
| `Post.find(id)` | `ctl.Store.GetPost(ctx, id)` |
| `post.save` | `store.CreatePost(ctx, post)` or `store.UpdatePost(ctx, post)`; validation runs inside |
| `SendWelcomeJob.perform_later(id)` | `app.Jobs.Insert(ctx, jobs.SendWelcomeArgs{ID: id}, nil)`; `InsertTx` inside a transaction |
| `Rails.logger.info` | `log.InfoContext(ctx, ...)`, a `*slog.Logger` passed in, never global |
| `Rails.env.production?` | `cfg.Env == config.EnvProduction` |
| `ENV["X"]`, `Rails.application.credentials.x` | a field on the `config.Config` struct, validated at boot |
| `request.request_id` | `c.GetString("request_id")`, set by the middleware |
| `Rails.cache` | none |
| `ActiveSupport::Notifications` | the request log line; OpenTelemetry rides in `ctx` when you add it |

## What is deliberately different

In Go these are explicit, and the six rules in [Conventions and agents](https://bogie-go.com/docs/conventions.html) keep them that way.

1. **Dependencies are passed in, in `app/application.go`.** Nothing reaches for a global. A controller gets its store on its wire line; a service's spec hands it a fake.
2. **Interfaces are declared by the consumer** (rule 2), not by the thing implemented. `postsStore` lives beside `PostsController` and lists only the five methods it calls. That keeps each interface short, and is why there is no mocking library.
3. **`ctx context.Context` is the first parameter** of anything that does I/O (rule 1). Cancellation, deadlines and tracing travel in it; a request that is abandoned stops its query.
4. **Constructors return `(T, error)`** (rule 4); only `main` exits. A bad configuration fails at boot with a message, never with a panic in a handler.
5. **`*gin.Context` stops at `app/controllers`** (rule 5). A handler converts HTTP into model values, calls one function, and converts the result back. Below that, the code can be called from a job, a CLI or a test.
6. **Registration is written down.** A controller exists once it is held, constructed and mounted, three lines above three `// bogie:` markers. The generators write them; `bogie doctor` checks them.

## What has no equivalent

Some are API-only by design; some are not needed in Go; some are gaps.

- Views, templates, the asset pipeline (no fingerprinting or compilation; `public/` serves files as they are), Turbo, sessions, flash, cookies, CSRF: the Rails app renders; this service is JSON.
- `rails console`.
- `ActiveRecord` associations, callbacks, scopes, `where` chains, N+1 helpers. SQL is written out and compiled.
- `db/schema.rb` and `db:schema:load`; the migrations are the schema.
- Fixtures and `test/test_helper.rb`. Specs truncate the test database and insert what they need, through factories you write by hand in `test/factories`.
- Concerns, helpers, `lib/tasks`. Plain packages and the Makefile.
- Mailers, Action Cable, Active Storage, I18n, caching. Wire a library when a service needs one; see the "glue, then gaps" rule in [docs/DESIGN.md](https://github.com/bogie-go/bogie/blob/main/docs/DESIGN.md).
- `rails g resource`, `rails g channel`, `rails g mailer`.
