如何编写带有ActiveRelation的UNION链? [英] How do I write a UNION chain with ActiveRelation?

查看:110
本文介绍了如何编写带有ActiveRelation的UNION链?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够使用ActiveRelation与UNION链接任意数量的子选择.

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

我对ARel的实现有点困惑,因为它似乎假定UNION是二进制操作.

I'm a little confused by the ARel implementation of this, since it seems to assume UNION is a binary operation.

但是:

( select_statement_a ) UNION ( select_statement_b ) UNION ( select_statement_c )

是有效的SQL.不用讨厌的字符串替换就可以吗?

is valid SQL. Is this possible without doing nasty string-substitution?

推荐答案

尽管亚当·拉塞克(Adam Lassek)处在正确的轨道上,但您可以做得比他建议的要好.我刚刚解决了一个类似的问题,试图从社交网络模型中获取朋友列表.可以通过多种方式自动获取好友,但我希望拥有一个ActiveRelation友好的查询方法,可以处理进一步的链接.所以我有

You can do a bit better than what Adam Lassek has proposed though he is on the right track. I've just solved a similar problem trying to get a friends list from a social network model. Friends can be aquired automatically in various ways but I would like to have an ActiveRelation friendly query method that can handle further chaining. So I have

class User
    has_many :events_as_owner, :class_name => "Event", :inverse_of => :owner, :foreign_key => :owner_id, :dependent => :destroy
    has_many :events_as_guest, :through => :invitations, :source => :event

      def friends


        friends_as_guests = User.joins{events_as_guest}.where{events_as_guest.owner_id==my{id}}
        friends_as_hosts  = User.joins{events_as_owner}.joins{invitations}.where{invitations.user_id==my{id}}

        User.where do
          (id.in friends_as_guests.select{id}
          ) | 
          (id.in friends_as_hosts.select{id}
          )
        end
       end

end

,它利用了Squeels子查询支持.生成的SQL是

which takes advantage of Squeels subquery support. Generated SQL is

SELECT "users".* 
FROM   "users" 
WHERE  (( "users"."id" IN (SELECT "users"."id" 
                           FROM   "users" 
                                  INNER JOIN "invitations" 
                                    ON "invitations"."user_id" = "users"."id" 
                                  INNER JOIN "events" 
                                    ON "events"."id" = "invitations"."event_id" 
                           WHERE  "events"."owner_id" = 87) 
           OR "users"."id" IN (SELECT "users"."id" 
                               FROM   "users" 
                                      INNER JOIN "events" 
                                        ON "events"."owner_id" = "users"."id" 
                                      INNER JOIN "invitations" 
                                        ON "invitations"."user_id" = 
                                           "users"."id" 
                               WHERE  "invitations"."user_id" = 87) )) 

通过对上面的代码稍加修改,展示了一个需要可变数量组件的替代模式

An alternative pattern where you need a variable number of components is demonstrated with a slight modification to the above code

  def friends


    friends_as_guests = User.joins{events_as_guest}.where{events_as_guest.owner_id==my{id}}
    friends_as_hosts  = User.joins{events_as_owner}.joins{invitations}.where{invitations.user_id==my{id}}

    components = [friends_as_guests, friends_as_hosts]

    User.where do
      components = components.map { |c| id.in c.select{id} }
      components.inject do |s, i|
        s | i
      end
    end


  end

这是关于OP确切问题的解决方案的粗略猜测

And here is a rough guess as to the solution for the OP's exact question

class Shift < ActiveRecord::Base
  def self.limit_per_day(options = {})
    options[:start]   ||= Date.today
    options[:stop]    ||= Date.today.next_month
    options[:per_day] ||= 5

    queries = (options[:start]..options[:stop]).map do |day|

      where{|s| s.scheduled_start >= day}.
      where{|s| s.scheduled_start < day.tomorrow}.
      limit(options[:per_day])

    end

    where do
      queries.map { |c| id.in c.select{id} }.inject do |s, i|
        s | i
      end
    end
  end
end

这篇关于如何编写带有ActiveRelation的UNION链?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆