通过组合多个列来组合结果集

ame*_*ior 2 ruby postgresql activerecord ruby-on-rails

我有这样的谈判表

id    seller_id  buyer_id property_id
1        4           190      33123
2        4           190      33123
3        5           191      34000
4        5           191      34000
5        6           200      35000
Run Code Online (Sandbox Code Playgroud)

我可以通过以下方式获取所有内容:

Negotiation.all
Run Code Online (Sandbox Code Playgroud)

我想取的一切,而是通过分组seller_id- buyer_id- property_id组合.在上面的例子中,我想返回三组.

这在我的rails应用程序中是否可行?

pot*_*hin 7

你可以使用Enumerable#group_by:

Negotiation.all.group_by{ |x| [x.seller_id, x.buyer_id, x.property_id] }
Run Code Online (Sandbox Code Playgroud)