model の中にこんなのを書く
1 2 3 4 5 6 7 8 |
class User < ActiveRecord::Base module UnionHack def union(*relations) from '((' + relations.map { |r| r.ast.to_sql }.join(') UNION (') + ')) AS ' + self.table_name end end extend UnionHack end |
そんで、以下のように使える。
1 |
User.union(@group1.users, @group2.users).limit(20) |
参考: http://coderwall.com/p/9hohaa