Skip to content

Instantly share code, notes, and snippets.

@nathansobo
Created March 3, 2011 02:34
Show Gist options
  • Save nathansobo/852218 to your computer and use it in GitHub Desktop.
Save nathansobo/852218 to your computer and use it in GitHub Desktop.
Keep specs excerpt -- 3-table right-associative join with 2 subqueries
describe "a right-associative 3-table inner join, with subqueries on either side" do
it "generates the appropriate sql" do
posts_comments = Post.join(Comment, Post[:id] => :post_id)
rel = Blog.where(:user_id => 1).join(posts_comments, Blog[:id] => :blog_id)
rel.to_sql.should be_like_query(%{
select t1.id as t1__id,
t1.user_id as t1__user_id,
t1.title as t1__title,
t2.posts__id as t2__posts__id,
t2.posts__blog_id as t2__posts__blog_id,
t2.posts__title as t2__posts__title,
t2.comments__id as t2__comments__id,
t2.comments__post_id as t2__comments__post_id,
t2.comments__body as t2__comments__body
from (select *
from blogs
where blogs.user_id = :v1) as t1
inner join (select posts.id as posts__id,
posts.blog_id as posts__blog_id,
posts.title as posts__title,
comments.id as comments__id,
comments.post_id as comments__post_id,
comments.body as comments__body
from posts
inner join comments
on posts.id = comments.post_id) as t2
on t1.id = t2.posts__blog_id
}, :v1 => 1)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment