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 per endpoint, declaring its own route |
params.require(:post).permit(:title) | A contract + validate — typed input, or an automatic 422 |
before_action / skip_before_action | before / skip |
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 templates and layouts |
head :no_content | render_empty status_code: 204 |
| Active Record models | Named SQL queries → a typed store |
Post.includes(:comments) | One batch query with ANY(...) (N+1) |
db/migrate/*.rb | Plain SQL files in db/migrations/ |
db/seeds.rb | seed_command in runcobo.yml |
| Request specs | Crystal spec + dispatch (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:
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
endIn Runcobo, each endpoint is a class with its route, its input and its response next to it:
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
endclass 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
endThere'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.
Strong parameters become contracts
A contract declares every field, its type and its constraints, and covers the path, query and body together:
class Posts::CreatePostContract < RequestValidation::Contract
schema do
required :title, String, filled: true, max_size: 200
required :body, String, filled: true
end
endinput.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:
-- name: GetPost :one
SELECT id, title, body, created_at FROM posts WHERE id = sqlc.arg(id);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) |
post.comments | A query for the comments, or a JOIN |
Post.includes(:comments) | WHERE post_id = ANY(...) + group_by (N+1) |
Post.transaction do | store.transaction do (recipe) |
| 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 page has more translations.
Migrations are 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.
Views
Jbuilder templates look like Rails', with explicit locals for partials:
json.id post.id
json.title post.title
json.partial! "posts/partials/author", author: authorThey can use the local variables of call (post above) — there are no @instance variables to pass. HTML uses 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 testand your deploy set — seeruncobo.yml. - No autoloading.
src/app.crrequires the app;require "./actions/**"picks up every action. - No sessions or CSRF tokens built in. Runcobo is API-first; see Security if you render forms.
- Exceptions aren't control flow. Lookups return
nil; render the404yourself. - Compile, then run.
cobo devrebuilds on every save and keeps the old version running if the new one doesn't compile.
Next: the blog tutorial shows a whole application.