How to use Sequel with ActiveRecord database
/ 2 min read
Sometimes we want to query rails database outside of a rails app. But should you always bring AR for this? No, lightweight Sequel can handle this job pretty well. Check the guide Querying in Sequel to understand basics.
- Add to Gemfile gems pg and sequel:
gem 'pg'gem 'sequel'- Create module
DBwhich contains AR models which you want to query. Sequel models have very similar syntax like ActiveRecord:
require 'pg'require 'sequel'
Sequel.default_timezone = :utc
module DB URL = Sequel.connect("postgres://user:password@localhost/database_name")
class Product < Sequel::Model(URL) plugin :timestamps, update_on_create: true many_to_one :brand end
class Brand < Sequel::Model(URL) plugin :timestamps, update_on_create: true one_to_many :products endendThis is it. Lets dig into details:
Sequel.default_timezone = :utcRails stores all dates in database using UTC format, where Sequel uses local time. We have to tell Sequel to use UTC instead.
URL = Sequel.connect("postgres://user:password@localhost/database_name")This is a DATABASE_URL, format same as in Active Record. It is a good idea to put this string to .env file using Dotenv and then: URL = Sequel.connect(ENV["DATABASE_URL"]).
plugin :timestamps, update_on_create: trueIf you want to create or update models as well, timestamps plugin will handle created_at and updated_at exact the same way as ActiveRecord.
Relations, where one_to_many equal to AR’s belongs_to and many_to_one equal to AR’s has_many. Read about Sequel relations more here.
And now you have everything to start:
product = DB::Product.firstproduct.brand# etc.There is full Sequel documentation http://sequel.jeremyevans.net/documentation.html.
Also, Sequel works with threads a bit simpler than ActiveRecord. If you ever tried to query AR inside threads, you know that connection pool can easy exceed and AR will raise an exception. That’s why in such cases all code which works with AR inside of threads should be wrapped to
ActiveRecord::Base.connection_pool.with_connection do # Do stuffendblock.
Sequel does this automatically for you. You don’t have to do anything, and this is great. Read about it more here: Why you should stop using ActiveRecord and start using Sequel (“Threading” part).