active_record_union icon indicating copy to clipboard operation
active_record_union copied to clipboard

Is flat union chaining possible? Is it preferable to nested?

Open bughit opened this issue 9 years ago • 3 comments

[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)

bughit avatar Sep 24 '15 15:09 bughit

looks like #4 enables this, do you plan on merging it?

bughit avatar Sep 24 '15 18:09 bughit

@brianhempel, what do you think of #4?

bughit avatar Oct 05 '15 22:10 bughit

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 .

brianhempel avatar Mar 20 '16 02:03 brianhempel