This project is archived and is in readonly mode.

#6591 new
clyfe

ActiveRecord UNION left out

Reported by clyfe | March 17th, 2011 @ 07:22 PM

ActiveRecord returns <Arel::Nodes::Union> when I call model.union(active_relation), but it should return an ActiveRelation

  1. What steps will reproduce the problem:

    class User << AR::Base; end
    User.where(:a => 1).union(User.where(:a => 2))

  2. What is the wrong result:

    The result is an instance of Arel::Nodes::Union

  3. What is the result that should happen instead:

    The computation should return an instance of ActiveRelation

THE GOOD

The resulting Arel::Nodes::Union object can be transformed to_sql and then we can use User.find_by_sql(sql)

a = User.where(:a => 1).union(User.where(:a => 2))
sql = a.to_sql # ( SELECT "users".* FROM "users" WHERE "users"."a" = 1 UNION SELECT "users".* FROM "users" WHERE "users"."a" = 2 )"
User.find_by_sql(sql)

THE ISSUE

The issue is obtaining an ActiveRelation object that we can further chain.
Caling a method (select, where, includes) on this (ActiveRelation unioned) object would have the behavior of further calling that method on each of the ActiveRelation objects involved in the UNION
________________________________________

All tough in general a UNION query can be avoided, there are some cases where the corect active relation #union functionality is needed.

For example this wold allow fixing issue #213 of CanCan https://github.com/ryanb/cancan/issues/213

I would be glad to work in this issue, with a little help.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Pages