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:
- In Terminal type
gem list
to list all Ruby Gems that are installed. If you don’t seeactiverecord
,sinatra-activerecord
, andsqlite3
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
- Create an empty
views
directory in the root directory (active-record
) - 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
- Open Terminal, navigate inside the root directory (
active-record
), and go into the Interactive Ruby Shell (IRB), by doing:
irb
- Next, inside IRB type
require "./server"
to load theserver.rb
file. - Then,
User.connection
to create a connection to the database. - Still in IRB enter
User
and you should see aUser(table doesn't exist)
response and arestful_routes_development.sqlite3
file created in the root directory, which is the database. - Finally, exit IRB by typing
exit
. - In the root directory, create a file called
Rakefile
. Note thatRakefile
doesn’t have a file extension! InsideRakefile
add the following code:require "./server" require "sinatra/activerecord/rake"
- 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
- 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
- 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 theusers
table was successfully created. - 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
- While still in IRB, to instantiate a user do:
u = User.new
- 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"
- To save these row values into the
users
table do:
u.save
- To see all rows entered into the
users
table do:
User.all
- To find the user with an
id
of1
in theusers
table do:
User.find(1)
- To create a new user faster than previously mentioned, do:
User.create(first_name: "Reggie", last_name: "Miller")
- To update the user with an
id
of1
, do the code below and press enter after each line:
u = User.find(1)
u.update(first_name: "Metta", last_name: "World Peace")
- To delete a user with an
id
of2
, do:
u = User.find(2)
u.destroy