I have a model "MyModel" with the following relationships:
Class MyModel
#if MyModel < ActiveRecord::Base
has_one a
has_one b
#if MyModel < Sequel::Model
many_to_one a
many_to_one b
end
With ActiveRecord:
MyModel.joins(:a, :b)
or
MyModel.joins(:a).joins(:b)
gives the following SQL:
SELECT * FROM my_models INNER JOIN bs ON bs.id = my_models.b_id INNER JOIN as ON a.id = my_models.a_id
But with Sequel:
MyModel.join(:as, id: :a_id).join(:bs, id: :b_id)
gives the following SQL:
SELECT * FROM my_models INNER JOIN bs ON bs.id = my_models.b_id INNER JOIN as ON a.id = bs.a_id
Why does join
use the last joined table's name? How do I get the same SQL generated by ActiveRecord in Sequel?
By default in Sequel's join
, the implicit qualifier is the last joined table.
So,
MyModel.join(:as, id: :a_id).join(:bs, id: :b_id)
equals:
MyModel.join(:as, id: :a_id).join(:bs, id: Sequel[:as][:b_id])
Use an explicit qualifier:
MyModel.join(:as, id: :a_id).join(:bs, id: Sequel[:my_models][:b_id])
This is from the official Sequel IRC chat with @jeremyevans.