合并两个 where 查询
Combine two where queries
如何合并这两个 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)
为什么合并是 return 空数组很简单,因为它在查询之间使用 AND
试试这个:
@activity = (activity1 + activity2)
如何合并这两个 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)
为什么合并是 return 空数组很简单,因为它在查询之间使用 AND
试试这个:
@activity = (activity1 + activity2)