Pakyow for Rails Developers: Connect to your database with Active Record

So you have decided to check out pakyow, the Ruby Web Framework that encourages view first development that has realtime built right into it. If you are a long time Rails developer who has stuck very close to the rails, you might not feel comfortable jumping into a new framework head first.

Pakyow encourages the use of the sequel gem to connect to the database, but as long as models respond to a #[] type accessor method, most of the default pakyow goodies can be used with any ORM.

I like ActiveRecord. It can be confusing and deep. There is no doubt that a lot of it is magic. And it’s just full of wonderful conventions. When I started working with pakyow, I stuck to ActiveRecord because it was what I was used to. I was able to dive deeper into the particularities of pakyow than I probably would have been able to if I had to learn a new ORM on top of it all.

Setting up ActiveRecord with pakyow is super simple. There are only 3 files you’ll have to touch in order to get things started.

First of all, you will have to make a decision: do you want to make decisions? Or would you rather keep it simple and follow Rails conventions? Let’s stick with the conventions.

The first thing we will have to touch is our config/database.yml file:

# config/database.yml
default: &default
adapter: sqlite3
encoding: utf-8
pool: 5
test:
<<: *default
database: db/test.sqlite3
development:
<<: *default
database: db/development.sqlite3

We will have to load this config in ActiveRecord in order for our models to be able to connect to the database. Notice that I am using the sqlite3 adapter. You can use whatever you want, but just make sure that you include the proper gem in your Gemfile.

Next thing on the list is loading the config, so that your models can access the database. Open up the app/setup.rb file and add these lines:

# app/setup.rb
require 'bundler/setup'
require 'pakyow'
require 'active_record'
Pakyow::App.define do
configure :global do
config_file = File.expand_path(
'../config/database.yml', __dir__
)
ActiveRecord::Base.configurations = YAML.load_file(config_file)
end
  configure :development do
ActiveRecord.establish_connection(:development)
end

configure :test do
ActiveRecord.establish_connection(:test)
end
end

The last bit of setup is not strictly necessary, but one of the best things about ActiveRecord is its rake tasks, so lets set up the basics (create/migrate/drop).

Open up your Rakefile. We are going to set up ActiveRecord’s DatabaseTasks object and make sure that we load pakyow, so the ActiveRecord::Base connection is established properly for the tasks.

# Rakefile
require 'rake'
require 'pakyow-rake'
require 'active_record'
include ActiveRecord::Tasks
task environment: [:'pakyow:prepare'] do
DatabaseTasks.env = ENV["RAILS_ENV"] || 'development'
DatabaseTasks.database_configuration = YAML.load_file(
File.expand_path('config/database.yml', __dir__)
)
DatabaseTasks.db_dir = 'db'
DatabaseTasks.migrations_paths = [
File.expand_path('db/migrate', __dir__)
]
DatabaseTasks.root = File.expand_path(__dir__)
end
namespace :db do
task drop: :environment do
DatabaseTasks.drop_current
end
  task create: :environment do
DatabaseTasks.create_current
end
  task migrate: :environment do
DatabaseTasks.migrate
end
end

This will let you setup, migrate and teardown your databases, just like you would do in Rails. Notice that you can specify different directories for the db_dir and also migration_paths can be any number of directories with ActiveRecord::Migration files.

Just as a quick side note, even though it wasn’t covered here, you can put your models anywhere in the app/lib directory, since everything there is (recursively) loaded automatically (in ascending order by filename)

I hope that this helps anyone who is excited to try out pakyow using ActiveRecord as an ORM. If you decide to add pakyow to your production stack, then you probably should check out sequel, since the developers of pakyow recommend using it.