Sinatra ActiveRecord Extension

Extends Sinatra with extension methods and Rake tasks for dealing with an SQL database using the ActiveRecord ORM.

ActiveRecord 4.1

For ActiveRecord 4.1 use sinatra-activerecord 2.0.0.rc2. I recommend upgrading even if you don't use ActiveRecord 4.1, because it includes all the Rake tasks that ActiveRecord has.


Put it in your Gemfile, along with the adapter of your database. For simplicity, let's assume you're using SQLite:

gem "sinatra-activerecord"
gem "sqlite3"
gem "rake"

Now require it in your Sinatra application, and establish the database connection:

# app.rb
require "sinatra/activerecord"

set :database, "sqlite3:///foo.sqlite3"

Alternatively, you can set the database with a hash or a YAML file. Take a look at this wiki.

Note that in modular Sinatra applications you will need to first register the extension:

class YourApplication < Sinatra::Base
  register Sinatra::ActiveRecordExtension

Now require the rake tasks and your app in your Rakefile:

# Rakefile
require "sinatra/activerecord/rake"
require "./app"

In the Terminal test that it works:

$ rake -T
rake db:create                     # create the database from config/database.yml from the current Sinatra env
rake db:create_migration           # create an ActiveRecord migration
rake db:drop                       # drops the data from config/database.yml from the current Sinatra env
rake db:migrate                    # migrate the database (use version with VERSION=n)
rake db:migrate:reset              # drops and creates the database and then runs the migrations
rake db:reset                      # drops and creates the database
rake db:rollback                   # roll back the migration (use steps with STEP=n)
rake db:schema:dump                # dump schema into file
rake db:schema:load                # load schema into database
rake db:seed                       # load the seed data from db/seeds.rb
rake db:setup                      # create the database and load the schema

And that's it, you're all set :)


You can create a migration:

$ rake db:create_migration NAME=create_users

This will create a migration file in your migrations directory (./db/migrate by default), ready for editing.

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :name

Now migrate the database:

$ rake db:migrate

You can also write models:

class User < ActiveRecord::Base
  validates_presence_of :name

You can put your models anywhere you want, only remember to require them if they're in a separate file, and that they're loaded after require "sinatra/activerecord".

Now everything just works:

get '/users' do
  @users = User.all
  erb :index

get '/users/:id' do
  @user = User.find(params[:id])
  erb :show

A nice thing is that the ActiveRecord::Base class is available to you through the database variable:

if database.table_exists?('users')
  # Do stuff
  raise "The table 'users' doesn't exist."


This gem was made in 2009 by Blake Mizerany, creator of Sinatra.


You can follow me on Twitter, I'm @jankomarohnic.