将两个查询组合在一起

时间:2015-10-23 16:47:09

标签: ruby-on-rails ruby ruby-on-rails-4 activerecord public-activity

如何合并这两个ActiveRecord关系?

activity1 = PublicActivity::Activity.where("user_recipients LIKE ':id,%' or user_recipients LIKE '%, :id' or user_recipients LIKE '%, :id,%' or user_recipients = ':id'", id: current_user.id)

activity2 = PublicActivity::Activity.where(recipient_id: current_user.id, recipient_type: 'User')

当我这样做时,如果一个为空,则默认为空

@activity = activity1.merge(activity2)

1 个答案:

答案 0 :(得分:2)

为什么合并返回空数组很简单,因为它在查询之间使用AND试试这个:

@activity = (activity1 + activity2)