0

我试图从关系中获取唯一元素,但它们由成对的 ID 来区分。以下示例说明了我想要做什么:

array.uniq {|post| post.a_id && post.b_id }

不幸的是,上面只会返回同时具有唯一 a_id 和 b_id 的元素。那不是我想要的。我希望它能够将这对独特的一对还给我。因此,如果我有以下分别带有 a_id 和 b_id 的帖子(如下所示),我希望它只返回 post1、post3 和 post4。

post1 1 2
post2 1 2
post3 1 3
post4 2 3

提前感谢所有帮助。

4

1 回答 1

0

这是你想要的?

array.uniq {|post| [ post.a_id, post.b_id ] }
于 2013-11-14T01:27:14.127 回答