Rodrigo Rosenfeld Rosas
Getting started with Sequel in Rails
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.
- Add “gem ‘sequel’” to your Gemfile
- Create an initializer, like config/initializers/setup-sequel.rb (see example below)
- Create your models (see example below)
# 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:
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:
# 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:
# 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:
# 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:
# 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.