---
url: /runcobo/migrate/rails.md
description: >-
  Coming from Ruby on Rails? How controllers, strong parameters, filters, Active
  Record, migrations and the rails command map to Runcobo.
---

# Coming from Rails

Runcobo will feel familiar: actions with `before` and `after` filters, Jbuilder views, migrations, a console and one command to run the app. The big differences come from Crystal being compiled — routes, input types, templates and SQL are all checked before the app starts — and from Runcobo using SQL instead of Active Record.

## At a glance

| Rails | Runcobo |
| --- | --- |
| `config/routes.rb` + a controller method | One [action class](/guide/actions) per endpoint, declaring its own route |
| `params.require(:post).permit(:title)` | A [contract](/guide/contracts) + `validate` — typed input, or an automatic `422` |
| `before_action` / `skip_before_action` | [`before` / `skip`](/guide/filters) |
| `after_action` | `after` |
| `ApplicationController` | `Blog::BaseAction` |
| `render json: post` | `render_json(...)`, or a `.jbuilder` template with `render` |
| Jbuilder views | Jbuilder templates — same idea, compiled in |
| ERB views and layouts | [Water](/guide/views#water) templates and layouts |
| `head :no_content` | `render_empty status_code: 204` |
| Active Record models | Named SQL queries → a typed [`store`](/guide/database) |
| `Post.includes(:comments)` | One batch query with `ANY(...)` ([N+1](/guide/n-plus-one)) |
| `db/migrate/*.rb` | Plain SQL files in `db/migrations/` |
| `db/seeds.rb` | `seed_command` in `runcobo.yml` |
| Request specs | Crystal `spec` + `dispatch` ([Testing](/guide/testing)) |

| Command | Runcobo |
| --- | --- |
| `rails new blog` | `crystal init app blog` + `bin/cobo init` |
| `rails server` | `bin/cobo dev` (rebuilds and restarts on save) |
| `rails console` | `bin/cobo console` |
| `rails runner script.rb` | `bin/cobo runner script.cr` |
| `rails routes` | `bin/cobo routes` |
| `rails db:create db:migrate` | `bin/cobo db prepare` |
| `rails db:migrate` / `db:rollback` | `bin/cobo db migrate` / `db rollback` |
| `rails test` / `rspec` | `bin/cobo test` |

## A controller becomes actions

In Rails, one controller holds every endpoint for a resource:

```ruby [app/controllers/posts_controller.rb]
class PostsController < ApplicationController
  before_action :authenticate, only: [:create]

  def show
    @post = Post.find(params[:id])
  rescue ActiveRecord::RecordNotFound
    render json: {error: "post_not_found"}, status: :not_found
  end

  def create
    post = Post.create!(params.require(:post).permit(:title, :body))
    render json: post, status: :created
  end
end
```

In Runcobo, each endpoint is a class with its route, its input and its response next to it:

```crystal [src/actions/posts/show_post.cr]
class Posts::ShowPost < Blog::BaseAction
  get "/posts/:id"

  call do
    input = validate(ShowPostContract)          # id is an Int64, or the client gets a 422
    if post = store.get_post?(id: input.id)     # nil when not found: no exception
      render                                    # → show_post.jbuilder
    else
      render_json({error: "post_not_found"}, status_code: 404)
    end
  end
end
```

```crystal [src/actions/posts/create_post.cr]
class Posts::CreatePost < Blog::AdminAction     # the admin base class authenticates
  post "/posts"

  call do
    input = validate(CreatePostContract)
    post = store.create_post(title: input.title, body: input.body)
    render status_code: 201
  end
end
```

There's no `only:`/`except:` on filters: actions that need authentication inherit from a base class that has it, and the rest don't. See [Filters](/guide/filters).

## Strong parameters become contracts

A contract declares every field, its type and its constraints, and covers the path, query and body together:

```crystal [src/actions/posts/create_post_contract.cr]
class Posts::CreatePostContract < RequestValidation::Contract
  schema do
    required :title, String, filled: true, max_size: 200
    required :body, String, filled: true
  end
end
```

`input.title` is a `String`, not a value you still have to check. Invalid input never reaches your code: the client gets a `422` listing every problem. Undeclared fields are dropped, so there's nothing to `permit`. JSON isn't wrapped in a root key unless you declare one.

## Active Record becomes SQL

Instead of models, you write the queries you need, and `bin/cobo generate` turns each into a typed method:

```sql [db/queries/posts.sql]
-- name: GetPost :one
SELECT id, title, body, created_at FROM posts WHERE id = sqlc.arg(id);
```

```crystal
store.get_post?(id: 1)   # => Blog::Store::GetPostRow? — title is a String, created_at a Time
```

| Active Record | Runcobo |
| --- | --- |
| `Post.find(id)` | `store.get_post?(id: id)` — `nil` instead of an exception |
| `Post.where(status: "draft").order(id: :desc)` | A named query with `WHERE` and `ORDER BY` |
| `post.update(title: "x")` | An `UPDATE ... RETURNING` query ([partial updates](/guide/query-recipes#partial-updates)) |
| `post.comments` | A query for the comments, or a `JOIN` |
| `Post.includes(:comments)` | `WHERE post_id = ANY(...)` + `group_by` ([N+1](/guide/n-plus-one)) |
| `Post.transaction do` | `store.transaction do` ([recipe](/guide/query-recipes#transactions)) |
| Model validations | Contracts for input, constraints in the schema for data |
| Callbacks | Explicit calls in the action, or SQL defaults and triggers |

The [Query Recipes](/guide/query-recipes#coming-from-active-record) page has more translations.

## Migrations are SQL

```sql [db/migrations/20261201093000_create_posts.sql]
-- migrate:up
CREATE TABLE posts (
  id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  title text NOT NULL,
  body text NOT NULL,
  created_at timestamptz NOT NULL DEFAULT now()
);

-- migrate:down
DROP TABLE posts;
```

There's no `schema.rb`: the migrations are the schema, and code generation reads them directly. See [Migrations](/cli/database).

## Views

Jbuilder templates look like Rails', with explicit locals for partials:

```crystal [src/actions/posts/show_post.jbuilder]
json.id post.id
json.title post.title
json.partial! "posts/partials/author", author: author
```

They can use the local variables of `call` (`post` above) — there are no `@instance` variables to pass. HTML uses [Water](/guide/views#water) instead of ERB. A missing template or a typo in a variable is a compile error.

## What's different

* **No environments in the app.** Configuration comes from environment variables, which `cobo dev`, `cobo test` and your deploy set — see [`runcobo.yml`](/cli/configuration).
* **No autoloading.** `src/app.cr` requires the app; `require "./actions/**"` picks up every action.
* **No sessions or CSRF tokens built in.** Runcobo is API-first; see [Security](/guide/security#csrf-and-cookies) if you render forms.
* **Exceptions aren't control flow.** Lookups return `nil`; render the `404` yourself.
* **Compile, then run.** `cobo dev` rebuilds on every save and keeps the old version running if the new one doesn't compile.

Next: the [blog tutorial](/guide/example) shows a whole application.
