Skip to content

Instantly share code, notes, and snippets.

@warmwaffles
Created March 27, 2013 21:42
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 warmwaffles/5258291 to your computer and use it in GitHub Desktop.
Save warmwaffles/5258291 to your computer and use it in GitHub Desktop.
subquery = Article.where(:person_id => 1).select(:id).order{id.desc}.limit(1)
Person.where(:id => 1).select{[id, name, subquery.as('last_article_id')]}.to_sql
=> SELECT "people"."id", "people"."name",
(
SELECT "articles"."id" FROM "articles"
WHERE "articles"."person_id" = 1
ORDER BY "articles"."id" DESC LIMIT 1
) last_article_id
FROM "people" WHERE "people"."id" = 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment