active_record_union
active_record_union copied to clipboard
Is flat union chaining possible? Is it preferable to nested?
[user_1.posts, user_2.posts, Post.published].inject(:union)
Nested
SELECT "posts".* FROM (
SELECT "posts".* FROM (
SELECT "posts".* FROM "posts" WHERE "posts"."user_id" = 1
UNION
SELECT "posts".* FROM "posts" WHERE "posts"."user_id" = 2
) posts
UNION
SELECT "posts".* FROM "posts" WHERE (published_at < '2014-07-19 16:12:45.882648')
) posts
Flat
SELECT "posts".* FROM (
SELECT "posts".* FROM "posts" WHERE "posts"."user_id" = 1
UNION
SELECT "posts".* FROM "posts" WHERE "posts"."user_id" = 2
UNION
SELECT "posts".* FROM "posts" WHERE (published_at < '2014-07-19 16:12:45.882648')
) posts
Flat chaining strikes me as potentially less problematic at least for some engines than deep nesting. Is it possible to achieve that in the current version? Would it make sense to add this?
Something like: Relation.union(user_1.posts, user_2.posts, Post.published)
looks like #4 enables this, do you plan on merging it?
@brianhempel, what do you think of #4?
Flatness would be great, but it's not easily supported by AREL right now so until that's improved it's going to be hacktastic to get it to work reliably in this gem.
See also discussion on #4 .