Skip to content

Instantly share code, notes, and snippets.

@nathansobo
Created March 21, 2011 06:32
Show Gist options
  • Save nathansobo/879116 to your computer and use it in GitHub Desktop.
Save nathansobo/879116 to your computer and use it in GitHub Desktop.
Complex prequel query subselects
Blog.where(:user_id => 1).join(Post).where(Post[:title] => "Foo").join(User).to_sql.should be_like_query(%{
select t1.t2__id as t1__t2__id,
t1.t2__user_id as t1__t2__user_id,
t1.t2__title as t1__t2__title,
t1.posts__id as t1__posts__id,
t1.posts__blog_id as t1__posts__blog_id,
t1.posts__title as t1__posts__title,
users.id as users__id
from (select t2.id as t2__id,
t2.user_id as t2__user_id,
t2.title as t2__title,
posts.id as posts__id,
posts.blog_id as posts__blog_id,
posts.title as posts__title
from (select *
from blogs
where blogs.user_id = :v1) as t2
inner join posts
on t2.id = posts.blog_id
where posts.title = :v2) as t1
inner join users
on users.id = t1.t2__user_id
}, :v1 => 1, :v2 => "Foo")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment