Intro

These are scribbling about my experience with ActiveRecord for Sinatra. This blog entry is a work in progress, to be updated and refined.

According to the official Rails docs “Active Record is the M in MVC architecture - the model - which is the layer of the system responsible for representing business data and logic.”

ActiveRecord is an Object Relational Mapper (ORM). It is basically a plug-in that allows us to map objects to databases.

The benefit of using an ORM is that we could switch it out and use any database that we feel is best suited to our needs, we could also plug in gems like ‘CarrierWave’ that would allow us to upload files more easily via active record. It might feel a little like we’re cheating, because we are not writing any SQL or model code, but the advantage is that it will be easier to migrate to using different databases, or multiple databases (if we would prefer a lighter DB, say, sqlite3 for testing purposes, and postgres for production).

We would do this plug-in via a set keyword in the controller.

In a Rails application we get ActiveRecord for free. In a Sinatra application, it is a little more involved.

I did find that using Sinatra-ActiveRecord allowed me a sort of a stepping stone from Sinatra to Rails.

Rails, in comparison to Sinatra, is a high level framework; we get lots of features and functionality for free.

Sinatra is a micro framework, where we have to craft things from the ground up. It is a more “low level” framework, and probably better suited to very small programs (i.e. microservices). The disadvantage of Sinatra is that it may be a little slower to get things up and running, it won’t be as scalable as Rails, higher latency when numerous users etc.

Rails on the other hand is a different story. It is a low latency, easily scalable, and hence more people are using it.

So what’s the advantage of Sinatra? Well, by comparison to Rails, it’s tiny! (less than 2000 lines of code), so it wont take up any space whatsoever.

In addition, when something goes wrong, it’s easier to fix because we’ll know where to find the problem having built everything from scratch.

How to

The steps:

In the terminal,

  1. To get a Gemfile, run:
bundle init
  1. Add these gems:
bundle add sinatra
  1. create server.rb and route:
require 'sinatra'

get '/' do
 "Hello World"
end
  1. Add gems:
bundle add sinatra-activerecord sqlite3 rake

sqlite3 is the database management system that we would like to use here (we could otherwise use postgres)

  1. Open server.rb:
#server.rb
require 'sinatra'
require 'sinatra/activerecord'

set :database, {adapter: "sqlite3", database: "my-image-app.sqlite3"}

#[...]

This plugs in the database that we choose to our application, it can also be done via a database.yml file, which might look something like this:

development:
  adapter: sqlite3
  database: db/development.sqlite3

test:
  adapter: sqlite3
  database: db/test.sqlite3

The ‘sinatra-activerecord’ gem will automtically look for this file when loading up.

  1. Create a builder like Rakefile:
#Rakefile
require 'sinatra/activerecord/rake'

namespace :db do
  task :load_config do
    require "./server"
  end
end

Do bundle exec rake -T to see list of rake commands 7. Create the database

bundle exec rake db:create

will create the database that we have set in the controller. See a cheatsheet for sqlite or postgres commands.

  1. Create the new migration:
bundle exec rake db:create migration create_users (or the name of the migration you want to create, in rails you do rails g model)

Extra resources and reading

A collection of blogs and resources that I used in researching how others had used the gem.

This is possibly the best blog that I found(?), written by another Maker: Walkthrough Chitter ORM

I used the sinatra-activerecord extension

I used several blog posts to inform my understanding: Creating a simple App with an MVC Framework with Sinatra and Active Record

This project was useful to consult, and compare the way to write the schema, though I think there may be a few errors, so I mostly consulted it lightly: project made by another developer

A video by CJ Avilla was pretty invaluable.

A blog post on Using Active Reocrd with Sinatra

In several places, I stumbled upon ‘Carrierwave’, as a “plug-in” to make file uppload easier or better (I have not used it yet and confirmed anything):

Carrierwave

This is a blog post on it specifically

Resources that referred to Rails and image upload:

How to add image upload functionality to your Rails app

This was pretty useful to check that I was writing the schema correctly, and also it is where I first saw about the file.read technique for uploading to public/uploads path. Uploader for Sinatra Portfolio Project

This is where I found it in the Rubydocs: https://ruby-doc.org/core-2.2.0/File.html