Rodrigo Rosenfeld Rosas

Getting started with Sequel in Rails

Wed, 18 Apr 2012 15:35:00 +0000 (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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# config/initializers/setup-sequel.rb
c = ActiveRecord::Base.configurations[Rails.env]
c['adapter'] = 'postgres' if c['adapter'] == 'postgresql'
c['user'] = c.delete 'username'
c['logger'] = [Rails.logger, Logger.new("log/#{Rails.env}_db.log")]
c['logger'] << Logger.new(STDOUT) if Rails.env.development?
DB = Sequel::Model.db = Sequel.connect c
Sequel::Model.db.sql_log_level = Rails.application.config.log_level || :info

if ARGV.any?{|p| p =~ /(--sandbox|-s)/}
  # do everything inside a transaction when using rails c --sandbox (or -s)
  DB.pool.after_connect = proc do |conn|
    DB.send(:add_transaction, conn, {})
    DB.send(:begin_transaction, conn, {})
  end
end

# Sequel::Model.plugin :active_model
# 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:

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

Finally, create your models:

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

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
2
3
4
5
6
7
# app/models/sq/user.rb
module SQ
  class User < Sequel::Model
    plugin :devise
    devise :database_authenticatable
  end
end

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
2
# config/routes.rb
devise_for :users, class_name: 'SQ::User'

RSpec

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

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

comments powered byDisqus