我正在使用PublicActivity gem,并希望将两个“where”查询与OR结合使用。每个查询都单独工作:
@activities = PublicActivity::Activity.where(owner_id: current_user.friend_ids, owner_type: "User")
@activities = PublicActivity::Activity.where(recipient_id: current_user.id, recipient_type: "User")
目标是:
返回owner_id为current_user.friend_ids OR 的活动,recipient_id为current_user.id。我试过这个,但它不起作用:
@activities = PublicActivity::Activity.where("owner_id LIKE ? OR recipient_id LIKE ?', current_user.friend_ids, current_user.id)
我已经尝试了squeel gem以及(...)。或.where(...)
有什么建议吗?
答案 0 :(得分:0)
like OR like
因错误的结果,误报等而臭名昭着。
如何使用:
@activities1 = PublicActivity::Activity.where(owner_id: current_user.friend_ids, owner_type: "User")
@activities2 = PublicActivity::Activity.where(recipient_id: current_user.id, recipient_type: "User")
@activities=(@activities1+@activities2).sort.unique
因为或将有效地区分两个记录集。 (刚添加.sort.uniq)
答案 1 :(得分:0)
这有效:
where("owner_id IN (?) OR recipient_id IN (?)', current_user.friend_ids, current_user.id)