Sinatra Activerecord Setup
Objectives
Setup a database in a Sinatra application.
Create and use a Rakefile to run ActiveRecord migrations.
Use ActiveRecord in a Sinatra application.
Overview
Sinatra doesn't come with database support out of the box, but it's relatively easy to configure. In general, we'll be working from templates that have this pre-built, but it's good to understand what's going on under the hood. We're going to practice adding a database to our Sinatra applications.
Instructions
Fork and clone this repository to get started! We have a basic sinatra application stubbed out with an app.rb
file acting as the controller.
Adding Your Gems
Currently, a few gems are already present in our Gemfile:
To get Sinatra working with ActiveRecord, First, we'll add three gems to allow us to use ActiveRecord: activerecord
version 5.2
, sinatra-activerecord
, and rake
. The activerecord
gem gives us access to the magical database mapping and association powers. The rake
gem, short for "ruby make", is a package that lets us quickly create files and folders, and automate tasks such as database creation, and the sinatra-activerecord
gem gives us access to some awesome Rake tasks. Make sure those three gems are added in your Gemfile:
Into our development group, we'll add two other gems: sqlite3
and tux
. sqlite3
is our database adapter gem - it's what allows our Ruby application to communicate with a SQL database. tux
will give us an interactive console that pre-loads our database and ActiveRecord relationships for us. Since we won't use either of these in production, we put them in our :development
group - this way, they won't get installed on our server when we deploy our application.
Our Gemfile is up to date - awesome! Go ahead and run bundle install
to get your system up to speed.
Connecting to the Database
We now have access to all of the gems that we need, but we still need to set up a connection to our database. Add the following block of code to your environment.rb
file (underneath Bundler.require(:default, ENV['SINATRA_ENV'])
).
This sets up a connection to a sqlite3 database named "database.db", located in a folder called "db." If we wanted our .db
file to be called dogs.db
, we could simply change the name of this file:
But for now, database.db
is a great name. Notice that this didn't actually create those files or folders yet - that's how Rake will help us.
Making a Rakefile
As we mentioned, rake
gives us the ability to quickly make files and set up automated tasks. We define these in a file called Rakefile
. First, create a Rakefile
in the root of our project directory. In the Rakefile
, we'll require our config/environment.rb
file to load up our environment, as well as "sinatra/activerecord/rake"
to get Rake tasks from the sinatra-activerecord
gem.
In the terminal, type rake -T
to view all of the available rake tasks. You should see the following output:
Testing it Out
Let's test out our handiwork by creating a dogs
table with two columns: name
and breed
. First, let's create our migration:
You should see the following output:
The beginning of the file is a timestamp - yours should reflect the time that your create_dogs
file was created! You've now created your first database migration inside of the db
folder.
Inside of the migration file, remove the default change
method (we'll come back to this), and add methods for up
and down
.
Important: When we create migrations with ActiveRecord, we must specify the version we're using just after ActiveRecord::Migration
. In this case, we're using 5.2
, so all the examples here will show ActiveRecord::Migration[5.2]
. This version may differ depending on the lab. If this number does not match the version in your Gemfile.lock
, your migration will cause an error.
Our up
method should create our table with name
and breed
columns. Our down method should drop the table.
Now, run the migration from the terminal with rake db:migrate
.
Why add SINATRA_ENV=development
, you might ask? Well, remember the top line of config/environment.rb
? It's telling Sinatra that it should use the "development" environment for both shotgun
and the testing suite. Therefore, we want to make sure our migrations run on the same environment as well, and specifying SINATRA_ENV=development
allows us to do that.
You should see the following output:
The change
Method
change
MethodThe change method is actually a shorter way of writing up
and down
methods. We can refactor our migration to look like this:
While the rollback (down
) method is not included, it's implicit in the change method. Rolling back the database would work in exactly the same way as using the down
method.
Last updated