Skip to content

Instantly share code, notes, and snippets.

@moro
Created April 2, 2013 05:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moro/5290187 to your computer and use it in GitHub Desktop.
Save moro/5290187 to your computer and use it in GitHub Desktop.
ummmm
['foo', 'bar'].inject(Paste.scoped) {|rel, tag|
rel.where(id: Tagging.joins(:tag).select(:paste_id).where('tags.name = ?', tag))
}.to_sql
# =>
# SELECT "pastes".*
# FROM "pastes"
# WHERE "pastes"."id" IN (SELECT paste_id FROM "taggings" INNER JOIN "tags" ON "tags"."id" = "taggings"."tag_id" WHERE (tags.name = 'foo'))
# AND "pastes"."id" IN (SELECT paste_id FROM "taggings" INNER JOIN "tags" ON "tags"."id" = "taggings"."tag_id" WHERE (tags.name = 'bar'))
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment