brianhempel / active_record_union

UNIONs in ActiveRecord! Adds proper union and union_all methods to ActiveRecord::Relation.
Other
423 stars 41 forks source link

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

Open bughit opened 8 years ago

bughit commented 8 years ago
[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 commented 8 years ago

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

bughit commented 8 years ago

@brianhempel, what do you think of #4?

brianhempel commented 8 years ago

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 .