1

我正在使用 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接收者 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(...).or.where(...)

有什么建议么?

4

2 回答 2

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)

于 2013-11-09T18:14:38.017 回答
0

这有效:

where("owner_id IN (?) OR recipient_id IN (?)', current_user.friend_ids, current_user.id)
于 2013-11-09T19:25:29.973 回答