0

This is my posting model. Posts have many user_ids through postings. I'd like to return the Posts that have user_id 2 and 3. So posts with ID 7 and 8.

Ideally my code would look something like this... but it doesn't work

Post.joins(:postings).where("postings.user_id = ?", [2,3])

id  | post_id | user_id | created_at                | updated_at                |
+---+---------+--------+---------------------------+---------------------------+
| 1 | 7       | 1      | 2013-09-07 16:03:50 -0400 | 2013-09-07 16:03:50 -0400 |
| 2 | 7       | 2      | 2013-09-07 16:03:50 -0400 | 2013-09-07 16:03:50 -0400 |
| 3 | 7       | 3      | 2013-09-07 16:03:50 -0400 | 2013-09-07 16:03:50 -0400 |
| 4 | 8       | 2      | 2013-09-07 22:17:49 -0400 | 2013-09-07 22:17:49 -0400 |
| 5 | 8       | 3      | 2013-09-07 22:17:49 -0400 | 2013-09-07 22:17:49 -0400 |
| 6 | 8       | 6      | 2013-09-07 22:17:49 -0400 | 2013-09-07 22:17:49 -0400 |
| 7 | 12      | 3      | 2013-09-14 12:49:56 -0400 | 2013-09-14 12:49:56 -0400 |

ADDED COMMENTS

  • I am using Rails
  • I'd like to be able to keep the [2,3] dynamic so there can be 10 entries in the array or 2

  • the Post table does not have a user_id column. The Posting model has a user_id column and the User table has a id column. The relationship between the two has been setup using a has_many relationship.

4

1 回答 1

1

我喜欢在子句中使用带有逻辑的聚合来解决这些问题having,因为这是解决这些类型问题的最灵活的方法:

select post_id
from postings
group by post_id
having sum(case when user_id = 2 then 1 else 0 end) > 0 and
       sum(case when user_id = 3 then 1 else 0 end) > 0;

子句中的每个条件都在having计算与每个用户 ID 匹配的记录数。

编辑:

根据您在表上的索引,对于这个特定问题,以下实际上可能会更好地执行:

select p2.user_id
from postings p2 join
     postings p3
     on p2.post_id = p3.post_id and
        p2.user_id = 2 and
        p3.user_id = 3;

编辑二:

对于动态列表,您可以执行以下操作:

select post_id
from postings
where user_id in (2, 3, 4, 5, 6)
group by post_id
having count(distinct user_id) = 5;
于 2013-09-18T03:15:34.183 回答