rails/arel — Issue #98: Can't do operations (order, limit) on a union.
Author: paul
Created: 2012-02-02 06:37:35+00:00
State: open

`#union` always returns a `Arel::Nodes::Union`, and you can't chain more operations off of it.

arel_a = arel_table_a.project(Arel.star)
arel_b = arel_table_b.project(Arel.star)

union = arel_a.union(arel_b)

union.class #=> Arel::Nodes::Union
union.order("x, y DESC") # Doesn't work

You either have to wrap the whole thing in a redundant Arel::SelectManager, which I haven't quite figured out yet, since the `AS` is required by postgres. I think I have to make it generate SQL like this, but that seems like a waste:

SELECT * FROM (
SELECT * FROM table_a UNION SELECT * FROM table_b
) AS table ORDER BY x, y DESC

Or do something like:

union.to_sql + " ORDER BY x, y DESC"

...except there's a comment on Nodes::Binary#to_sql that says it shouldn't be used.