Giter VIP home page Giter VIP logo

sinatra-activerecord's Introduction

Sinatra ActiveRecord Extension

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

Setup

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:

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
end

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

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

In the Terminal test that it works:

$ rake -T
rake db:create_migration  # create an ActiveRecord migration
rake db:migrate           # migrate the database (use version with VERSION=n)
rake db:rollback          # roll back the migration (use steps with STEP=n)

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

Usage

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
    end
  end
end

Now migrate the database:

$ rake db:migrate

You can also write models:

class User < ActiveRecord::Base
  validates_presence_of :name
end

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
end

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

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

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

History

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

Social

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

License

MIT

sinatra-activerecord's People

Contributors

janko avatar bmizerany avatar runemadsen avatar zenbaku avatar dikarel avatar nbudin avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.