Skip to content

Instantly share code, notes, and snippets.

@nathansobo
Created March 25, 2011 23:36
Show Gist options
  • Save nathansobo/887854 to your computer and use it in GitHub Desktop.
Save nathansobo/887854 to your computer and use it in GitHub Desktop.
A join to a union, which produces a subquery
(Blog.where(:user_id => 1) | Blog.where(:public => true)).join(Post).to_sql.should be_like_query(%{
select t1.id as t1__id,
t1.user_id as t1__user_id,
t1.public as t1__public,
posts.id as posts__id,
posts.blog_id as posts__blog_id
from ((select *
from blogs
where blogs.user_id = :v1)
union
(select *
from blogs
where blogs.public = :v2)) as t1
inner join posts
on t1.id = posts.blog_id
}, :v1 => 1, :v2 => true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment