How do I write a UNION chain with ActiveRelation?

后端 未结 4 538
小蘑菇
小蘑菇 2021-02-05 13:23

I need to be able to chain an arbitrary number of sub-selects with UNION using ActiveRelation.

I\'m a little confused by the ARel implementation of this, si

4条回答
  •  抹茶落季
    2021-02-05 13:38

    There's a way to make this work using arel:

    tc=TestColumn.arel_table
    return TestColumn.where(tc[:id]
               .in(TestColumn.select(:id)
                             .where(:attr1=>true)
                             .union(TestColumn.select(:id)
                                              .select(:id)
                                              .where(:attr2=>true))))
    

提交回复
热议问题