ransack
https://github.com/activerecord-hackery/ransack
Ruby
Object-based searching.
Ransack::Adapters::ActiveRecord::Context#build_correlated_subquery
Build an Arel subquery that selects keys for the top query,
drawn from the first join association's foreign_key.
Example: for an Article that has_and_belongs_to_many Tags
context = Article.search.context
attribute = Attribute.new(context, "tags_name").tap do |a|
context.bind(a, a.name)
end
context.build_correlated_subquery(attribute.parent).to_sql
# SELECT "articles_tags"."article_id" FROM "articles_tags"
# INNER JOIN "tags" ON "tags"."id" = "articles_tags"."tag_id"
# WHERE "articles_tags"."article_id" = "articles"."id"
The WHERE condition on this query makes it invalid by itself,
because it is correlated to the primary key on the outer query.Edit
git clone [email protected]:activerecord-hackery/ransack.git
cd ransack
open lib/ransack/adapters/active_record/context.rb
Contribute
# Make a new branchgit checkout -b -your-name--update-docs-Ransack--Adapters--ActiveRecord--Context-build_correlated_subquery-for-pr
# Commit to gitgit add lib/ransack/adapters/active_record/context.rbgit commit -m "better docs for Ransack::Adapters::ActiveRecord::Context#build_correlated_subquery"
# Open pull requestgem install hub # on a mac you can `brew install hub`
hub fork
git push <your name> -your-name--update-docs-Ransack--Adapters--ActiveRecord--Context-build_correlated_subquery-for-pr
hub pull-request
# Celebrate!