Rodrigo Rosenfeld Rosas

Getting started with Sequel in Rails

Wed, 18 Apr 2012 15:35:00 +0000 (last updated at Fri, 20 Dec 2013 10:25:00 +0000)

Why Sequel?

In short, I feel it is better designed than ActiveRecord and makes some non-trivial queries much easier to implement and read. Detailed information can be found here.

How to use Sequel models?

I didn't create any generator or gem for my application. It is just pretty simple to setup your environment.

  1. Add "gem 'sequel'" to your Gemfile
  2. Create an initializer, like config/initializers/setup-sequel.rb (see example below)
  3. Create your models (see example below)
1# config/initializers/setup-sequel.rb
2c = ActiveRecord::Base.configurations[Rails.env]
3c['adapter'] = 'postgres' if c['adapter'] == 'postgresql'
4c['user'] = c.delete 'username'
5c['logger'] = [Rails.logger, Logger.new("log/#{Rails.env}_db.log")]
6c['logger'] << Logger.new(STDOUT) if Rails.env.development?
7DB = Sequel::Model.db = Sequel.connect c
8Sequel::Model.db.sql_log_level = Rails.application.config.log_level || :info
9
10if ARGV.any?{|p| p =~ /(--sandbox|-s)/}
11 # do everything inside a transaction when using rails c --sandbox (or -s)
12 DB.pool.after_connect = proc do |conn|
13 DB.send(:add_transaction, conn, {})
14 DB.send(:begin_transaction, conn, {})
15 end
16end
17
18# Sequel::Model.plugin :active_model
19# Sequel::Model.plugin :validation_helpers

You can enable the available plugins directly in the initializer or in a per-class basis.

If you're using FactoryGirl, it requires the model classes to respond to 'save!', so you can add this to your initializer:

1module Sequel::Plugins::FactoryGirlSupport
2 module InstanceMethods
3 def save!
4 save_changes raise_on_save_failure: true
5 end
6 end
7end
8Sequel::Model.plugin Sequel::Plugins::FactoryGirlSupport # or plugin :factory_girl_support

Finally, create your models:

1# app/models/user.rb
2class User < Sequel::Model
3 # do whatever you want here
4end

If you're used to ActiveRecord you can take a look at Sequel for ActiveRecord Users.

Devise

If you want to use your Sequel model as a Devise authentication class, please take a look at the sequel-devise gem.

In short, just append "gem 'sequel-devise'" to your Gemfile (you'll also need the 'devise' gem if you're starting from scratch).

Then, enable your User class to be compatible with Devise. If you want to keep your current User class while you're giving this a try, just put it in another namespace, as in the example below:

1# app/models/sq/user.rb
2module SQ
3 class User < Sequel::Model
4 plugin :devise
5 devise :database_authenticatable
6 end
7end

Finally, in your routes, if you're using this namespaced User class, you'll need to adapt your devise_for statement to something like:

1# config/routes.rb
2devise_for :users, class_name: 'SQ::User'

RSpec

For running your examples inside database transactions, you can add this to your spec_helper.rb:

1 # setup transactional factory for Sequel
2 config.around(:each) do |example|
3 DB.transaction do
4 example.run
5 raise Sequel::Error::Rollback
6 end
7 end

Have fun

Feel free to leave any questions in the comments or to report any bugs to the sequel-devise gem.

If you're like me, you'll enjoy Sequel way better than ActiveRecord.

Powered by Disqus