Skip to content

Instantly share code, notes, and snippets.

@jtallant
Last active March 25, 2024 13:26
Show Gist options
  • Star 56 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save jtallant/fd66db19e078809dfe94401a0fc814d2 to your computer and use it in GitHub Desktop.
Save jtallant/fd66db19e078809dfe94401a0fc814d2 to your computer and use it in GitHub Desktop.
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
@Abidzar16
Copy link

hello, thanks for the post, I have following the instruction, but get this error when running ruby app.rb and accessing localhost

D, [2021-08-10T14:21:48.169884 #17552] DEBUG -- : (2.3ms) SELECT sqlite_version()
D, [2021-08-10T14:21:48.171606 #17552] DEBUG -- : User Load (0.4ms) SELECT "users".
FROM "users"
2021-08-10 14:21:48 - ActiveRecord::StatementInvalid - SQLite3::SQLException: no such table: users:

@m-axis
Copy link

m-axis commented Sep 14, 2021

hello, thanks for the post, I have following the instruction, but get this error when running ruby app.rb and accessing localhost

D, [2021-08-10T14:21:48.169884 #17552] DEBUG -- : (2.3ms) SELECT sqlite_version() D, [2021-08-10T14:21:48.171606 #17552] DEBUG -- : User Load (0.4ms) SELECT "users". FROM "users"
2021-08-10 14:21:48 - ActiveRecord::StatementInvalid - SQLite3::SQLException: no such table: users:

in app.rb replacing
set :database, "sqlite3:development.sqlite3"
with
set :database_file, "./config/database.yml"
worked for me.

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