class Ransack::Adapters::ActiveRecord::Context
def build_correlated_subquery(association)
because it is correlated to the primary key on the outer query.
The WHERE condition on this query makes it invalid by itself,
# WHERE "articles_tags"."article_id" = "articles"."id"
# INNER JOIN "tags" ON "tags"."id" = "articles_tags"."tag_id"
# SELECT "articles_tags"."article_id" FROM "articles_tags"
context.build_correlated_subquery(attribute.parent).to_sql
end
context.bind(a, a.name)
attribute = Attribute.new(context, "tags_name").tap do |a|
context = Article.search.context
Example: for an Article that has_and_belongs_to_many Tags
drawn from the first join association's foreign_key.
Build an Arel subquery that selects keys for the top query,
def build_correlated_subquery(association) join_constraints = extract_joins(association) join_root = join_constraints.shift correlated_key = extract_correlated_key(join_root) subquery = Arel::SelectManager.new(association.base_klass) subquery.from(join_root.left) subquery.project(correlated_key) join_constraints.each do |j| subquery.join_sources << Arel::Nodes::InnerJoin.new(j.left, j.right) end subquery.where(correlated_key.eq(primary_key)) end