Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Setting up Sinatra with Active Record

Setting up Sinatra Project

create an empty project and add a Gemfile

cd ~/Desktop
mkdir project-name
cd project-name
touch Gemfile
# Gemfile
source 'https://rubygems.org'

gem 'activerecord'
gem 'sinatra-activerecord'
gem 'sqlite3'
gem 'rake'

Install the dependencies

bundle install

Create an app.rb file

# app.rb
require 'sinatra'
require 'sinatra/activerecord'

set :database, "sqlite3:project-name.sqlite3"

Create a Rakefile

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

Create a migration for creating a users table

rake db:create_migration NAME=create_users_table

Add code to the migration for creating columns

class CreateUsersTable < ActiveRecord::Migration[5.0]
  def change
    create_table :users do |t|
      t.string :fname
      t.string :lname
      t.string :email
      t.datetime :created_at
      t.datetime :updated_at
    end
  end
end

Run the migration

rake db:migrate

Create a User model

# models.rb
class User < ActiveRecord::Base
end

Load the User model into your app

# at the bottom of app.rb
require './models'

Create a seeds file

touch db/seeds.rb

Write some seeds

# db/seeds.rb
users = [
  {fname: 'Jon', lname: 'Doe', email: 'e@example.com'},
  {fname: 'Jane', lname: 'Doe', email: 'e@example.com'}
]

users.each do |u|
  User.create(u)
end

Run the seeds

rake db:seed

Create an index.erb file in a views directory (views/index.erb)

<!DOCTYPE html>
<html>
<head>
    <title>Users</title>
</head>
<body>
    <ul>
        <% @users.each do |user| %>
            <li><%= user.email %></li>
        <% end %>
    </ul>
</body>
</html>

Create a route for the home page

# app.rb
get '/' do
  @users = User.all
  erb :index
end

Adding more tables (models)

  1. Create migration with rake
  2. Populate the migration with code for adding columns
  3. Run the migration with rake db:migrate
  4. Create the model (add class to models file)
  5. Add some rows to the table with IRB
  6. Create a route and a view for displaying records
@jedrekdomanski

This comment has been minimized.

Copy link

@jedrekdomanski jedrekdomanski commented Dec 30, 2017

When I do rake db:create_migration NAME=create_users_table it throws an error

rake aborted!
LoadError: cannot load such file -- active_record/railties/databases.rake
/home/jedrek/.rvm/gems/ruby-2.3.0/gems/sinatra-activerecord-2.0.13/lib/sinatra/activerecord/rake.rb:1:in `load'
/home/jedrek/.rvm/gems/ruby-2.3.0/gems/sinatra-activerecord-2.0.13/lib/sinatra/activerecord/rake.rb:1:in `<top (required)>'
/home/jedrek/workspace/sinatra-activerecord/Rakefile:1:in `<top (required)>'
/home/jedrek/.rvm/gems/ruby-2.3.0/gems/rake-12.3.0/exe/rake:27:in `<top (required)>'

Caused by:
LoadError: cannot load such file -- sinatra/activerecord/rake
/home/jedrek/workspace/sinatra-activerecord/Rakefile:1:in `<top (required)>'
/home/jedrek/.rvm/gems/ruby-2.3.0/gems/rake-12.3.0/exe/rake:27:in `<top (required)>'
(See full trace by running task with --trace)

Can you help me fix it?

@susemi99

This comment has been minimized.

Copy link

@susemi99 susemi99 commented Sep 19, 2018

@jedrekdomanski
add require 'sinatra/activerecord' to top of Rakefile.

@lassiter

This comment has been minimized.

Copy link

@lassiter lassiter commented Apr 11, 2019

@jedrekdomanski you need to use bundle exec.

@RillonDodgers

This comment has been minimized.

Copy link

@RillonDodgers RillonDodgers commented Mar 15, 2020

This is great, thank you.

@rickyzheng

This comment has been minimized.

Copy link

@rickyzheng rickyzheng commented May 21, 2020

I have the following error:

$ rake db:seed
rake aborted!
ActiveRecord::AdapterNotSpecified: The development database is not configured for the default_env environment.

Available databases configurations are:

/Users/ricky/.rvm/gems/ruby-2.6.5@freshgroupon/gems/activerecord-6.0.3.1/lib/active_record/connection_adapters/connection_specification.rb:250:in resolve_symbol_connection' /Users/ricky/.rvm/gems/ruby-2.6.5@freshgroupon/gems/activerecord-6.0.3.1/lib/active_record/connection_adapters/connection_specification.rb:218:in resolve_connection'
/Users/ricky/.rvm/gems/ruby-2.6.5@freshgroupon/gems/activerecord-6.0.3.1/lib/active_record/connection_adapters/connection_specification.rb:139:in resolve' /Users/ricky/.rvm/gems/ruby-2.6.5@freshgroupon/gems/activerecord-6.0.3.1/lib/active_record/connection_handling.rb:170:in resolve_config_for_connection'
/Users/ricky/.rvm/gems/ruby-2.6.5@freshgroupon/gems/activerecord-6.0.3.1/lib/active_record/connection_handling.rb:50:in establish_connection' /Users/ricky/.rvm/gems/ruby-2.6.5@freshgroupon/gems/activerecord-6.0.3.1/lib/active_record/railties/databases.rake:272:in block (2 levels) in <top (required)>'
/Users/ricky/.rvm/gems/ruby-2.6.5@freshgroupon/gems/activerecord-6.0.3.1/lib/active_record/railties/databases.rake:330:in block (2 levels) in <top (required)>' /Users/ricky/.rvm/gems/ruby-2.6.5@freshgroupon/gems/rake-13.0.1/exe/rake:27:in <top (required)>'
/Users/ricky/.rvm/gems/ruby-2.6.5@freshgroupon/bin/ruby_executable_hooks:24:in eval' /Users/ricky/.rvm/gems/ruby-2.6.5@freshgroupon/bin/ruby_executable_hooks:24:in

'
Tasks: TOP => db:abort_if_pending_migrations
(See full trace by running task with --trace)

@rafa-sr

This comment has been minimized.

Copy link

@rafa-sr rafa-sr commented Jul 24, 2020

hello thx for the post, if i want to remove a migration what should i type?

@ruvaleev

This comment has been minimized.

Copy link

@ruvaleev ruvaleev commented Aug 15, 2020

hello thx for the post, if i want to remove a migration what should i type?

rake db:rollback

@ShayneP

This comment has been minimized.

Copy link

@ShayneP ShayneP commented Sep 7, 2020

I have the following error:

$ rake db:seed
rake aborted!
ActiveRecord::AdapterNotSpecified: The development database is not configured for the default_env environment.

Available databases configurations are:

/Users/ricky/.rvm/gems/ruby-2.6.5@freshgroupon/gems/activerecord-6.0.3.1/lib/active_record/connection_adapters/connection_specification.rb:250:in resolve_symbol_connection' /Users/ricky/.rvm/gems/ruby-2.6.5@freshgroupon/gems/activerecord-6.0.3.1/lib/active_record/connection_adapters/connection_specification.rb:218:in resolve_connection'
/Users/ricky/.rvm/gems/ruby-2.6.5@freshgroupon/gems/activerecord-6.0.3.1/lib/active_record/connection_adapters/connection_specification.rb:139:in resolve' /Users/ricky/.rvm/gems/ruby-2.6.5@freshgroupon/gems/activerecord-6.0.3.1/lib/active_record/connection_handling.rb:170:in resolve_config_for_connection'
/Users/ricky/.rvm/gems/ruby-2.6.5@freshgroupon/gems/activerecord-6.0.3.1/lib/active_record/connection_handling.rb:50:in establish_connection' /Users/ricky/.rvm/gems/ruby-2.6.5@freshgroupon/gems/activerecord-6.0.3.1/lib/active_record/railties/databases.rake:272:in block (2 levels) in <top (required)>'
/Users/ricky/.rvm/gems/ruby-2.6.5@freshgroupon/gems/activerecord-6.0.3.1/lib/active_record/railties/databases.rake:330:in block (2 levels) in <top (required)>' /Users/ricky/.rvm/gems/ruby-2.6.5@freshgroupon/gems/rake-13.0.1/exe/rake:27:in <top (required)>'
/Users/ricky/.rvm/gems/ruby-2.6.5@freshgroupon/bin/ruby_executable_hooks:24:in eval' /Users/ricky/.rvm/gems/ruby-2.6.5@freshgroupon/bin/ruby_executable_hooks:24:in

'
Tasks: TOP => db:abort_if_pending_migrations
(See full trace by running task with --trace)

From your app folder:

mkdir config
touch config/database.yml

Go into the database.yml file, add this :

development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 50
  write_timeout: 300
  read_timeout: 300

Then in your app.rb file:

set :database, "sqlite3:development.sqlite3"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment