Skip to content

Instantly share code, notes, and snippets.

@ernie
Created April 23, 2010 14:28
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 ernie/376585 to your computer and use it in GitHub Desktop.
Save ernie/376585 to your computer and use it in GitHub Desktop.
Autojoin sample results:
a = Article.includes(:comments).where(:comments => {:moderations => {:value => 1},:body => 'hey'}).autojoin
Article Load (0.4ms) SELECT "articles".* FROM "articles" INNER JOIN "comments" ON "comments"."article_id" = "articles"."id" INNER JOIN "moderations" ON "moderations"."comment_id" = "comments"."id" WHERE (("moderations"."value" = 1 AND "comments"."body" = 'hey'))
Comment Load (0.4ms) SELECT "comments".* FROM "comments" WHERE ("comments".article_id = 1)
a = Article.eager_load(:moderations).where(:comments => {:moderations => {:value => 1},:body => 'hey'}).autojoin.first
Article Load (0.3ms) SELECT DISTINCT "articles".id FROM "articles" LEFT OUTER JOIN "comments" ON "articles"."id" = "comments"."article_id" LEFT OUTER JOIN "moderations" ON "moderations"."comment_id" = "comments"."id" INNER JOIN "comments" "comments_articles" ON "comments_articles"."article_id" = "articles"."id" INNER JOIN "moderations" "moderations_comments" ON "moderations_comments"."comment_id" = "comments_articles"."id" WHERE (("moderations_comments"."value" = 1 AND "comments_articles"."body" = 'hey')) LIMIT 1
Article Load (1.2ms) SELECT "articles"."id" AS t0_r0, "articles"."title" AS t0_r1, "articles"."body" AS t0_r2, "articles"."created_at" AS t0_r3, "articles"."updated_at" AS t0_r4, "articles"."lookup_id" AS t0_r5, "moderations"."id" AS t1_r0, "moderations"."comment_id" AS t1_r1, "moderations"."value" AS t1_r2, "moderations"."created_at" AS t1_r3, "moderations"."updated_at" AS t1_r4 FROM "articles" LEFT OUTER JOIN "comments" ON "articles"."id" = "comments"."article_id" LEFT OUTER JOIN "moderations" ON "moderations"."comment_id" = "comments"."id" INNER JOIN "comments" "comments_articles" ON "comments_articles"."article_id" = "articles"."id" INNER JOIN "moderations" "moderations_comments" ON "moderations_comments"."comment_id" = "comments_articles"."id" WHERE (("moderations_comments"."value" = 1 AND "comments_articles"."body" = 'hey')) AND ("articles"."id" IN (1))
a.moderations
=> [#<Moderation id: 1, comment_id: 1, value: 1, created_at: "2010-03-04 19:49:04", updated_at: "2010-03-04 19:49:04">]
No DB query.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment