Blog

Basic Active Record Database Set Up Using Sinatra Ruby Framework
Posted on August 8, 2015 in Ruby, Sinatra by Matt Jennings

Assuming your inside a directory called active-record, follow the steps below:

  1. In Terminal type gem list to list all Ruby Gems that are installed. If you don’t see activerecord, sinatra-activerecord, and sqlite3 installed type in the comments in Mac Terminal and press enter after each command to install these gems:
    gem install activerecord
    gem install sinatra-activerecord
    gem install sqlite3
  2. Create an empty views directory in the root directory (active-record)
  3. Create a server.rb file in the root directory with the following code:
    require "sinatra"
    require "active_record"
    require "sqlite3"
    
    # The "ActiveRecord" object has a "Base" class which in turn has a
    # "establish_connection" method which below creates a
    # "sqlite3" database connection and will eventually
    # create a database named "restful_routes_development.sqlite3" in the root directory
    ActiveRecord::Base.establish_connection(
      :adapter => "sqlite3",
      :database => "restful_routes_development.sqlite3"
    )
    
    # "User" class inherits from "ActiveRecord::Base"
    class User < ActiveRecord::Base
    end
    
    get "/" do
      "Frank Sinatra"
    end
  4. Open Terminal, navigate inside the root directory (active-record), and go into the Interactive Ruby Shell (IRB), by doing:
    irb
  5. Next, inside IRB type require "./server" to load the server.rb file.
  6. Then, User.connection to create a connection to the database.
  7. Still in IRB enter User and you should see a User(table doesn't exist) response and a restful_routes_development.sqlite3 file created in the root directory, which is the database.
  8. Finally, exit IRB by typing exit.
  9. In the root directory, create a file called Rakefile. Note that Rakefile doesn’t have a file extension! Inside Rakefile add the following code:
    require "./server"
    require "sinatra/activerecord/rake"
  10. Reopen Terminal, navigate inside the root directory (active-record), make sure you are not in IRB, and type the code below to start the migration:
    rake db:create_migration NAME=create_users

    Now a in your root directory a file name in the location similar to below will be created. In this file name the “20150808223300” string is generated based on the time the file was created.
    /db/migrate/20150808223300_create_users.rb

  11. Open the file that has a similar name to /db/migrate/20150808223300_create_users.rb, and ensure its code looks like below:
    # The "CreateUsers" class will create a "users" table
    class CreateUsers < ActiveRecord::Migration
      def change
        create_table :users do |t|
          # ":first_name" and ":last_name" symbols create
          # "first_name" and "last_name" column names
          t.string :first_name
          t.string :last_name
          # The line below creates "created_at" and "updated_at"
          # columns that are filled with the appropriate times
          t.timestamps null: false
        end
      end
    end
  12. Finally, to create the users table open Terminal and type:
    rake db:migrate

    The Terminal output will have something like create_table(:users) to show that the users table was successfully created.

  13. To perform CRUD operations on the table, first go into IRB and typing in the following commands in Terminal pressing return after each one:
    irb
    require "./server"
    User.connection
  14. While still in IRB, to instantiate a user do:
    u = User.new
  15. Then, to add a new user’s first and last names to the users table do something like below, pressing enter after each line in IRB:
    u.first_name = "Ron"
    u.last_name = "Artest"
  16. To save these row values into the users table do:
    u.save
  17. To see all rows entered into the users table do:
    User.all
  18. To find the user with an id of 1 in the users table do:
    User.find(1)
  19. To create a new user faster than previously mentioned, do:
    User.create(first_name: "Reggie", last_name: "Miller")
  20. To update the user with an id of 1, do the code below and press enter after each line:
    u = User.find(1)
    u.update(first_name: "Metta", last_name: "World Peace")
  21. To delete a user with an id of 2, do:
    u = User.find(2)
    u.destroy

Leave a Reply

To Top ↑