从数组中提取单个案例

时间:2016-12-12 10:04:40

标签: ruby-on-rails ruby postgresql

生成的记录数组
+----+------+-------+------+------+------+------+------+------+------+------+------+------+------+------+-------+
| id | name | head  | apr  | may  | jun  | jul  | aug  | sep  | oct  | nov  | dec  | jan  | feb  | mar  | total |
+----+------+-------+------+------+------+------+------+------+------+------+------+------+------+------+-------+
| 1  | A    | basic | 1000 | 1000 | 1000 | 1000 | 1000 | 1000 | 1000 | 1000 | 1000 | 1000 | 1000 | 1000 | 12000 |
+----+------+-------+------+------+------+------+------+------+------+------+------+------+------+------+-------+
| 1  | A    | hra   | 500  | 500  | 500  | 500  | 500  | 500  | 500  | 500  | 500  | 500  | 500  | 500  | 6000  |
+----+------+-------+------+------+------+------+------+------+------+------+------+------+------+------+-------+
| 2  | B    | basic | 2000 | 2000 | 2000 | 2000 | 2000 | 2000 | 2000 | 2000 | 2000 | 0    | 0    | 2000 | 24000 |
+----+------+-------+------+------+------+------+------+------+------+------+------+------+------+------+-------+
| 2  | B    | hra   | 500  | 500  | 500  | 500  | 500  | 500  | 500  | 500  | 500  | 0    | 0    | 500  | 6000  |
+----+------+-------+------+------+------+------+------+------+------+------+------+------+------+------+-------+
| 3  | C    | basic | 5000 | 5000 | 5000 | 5000 | 5000 | 5000 | 0    | 0    | 0    | 5000 | 5000 | 5000 | 60000 |
+----+------+-------+------+------+------+------+------+------+------+------+------+------+------+------+-------+
| 3  | C    | hra   | 500  | 500  | 500  | 500  | 500  | 500  | 0    | 0    | 0    | 500  | 500  | 500  | 6000  |
+----+------+-------+------+------+------+------+------+------+------+------+------+------+------+------+-------+

将包含一个或多个签名的操作。

@signatures = Signature.where('action_id IN (?)', @actions).all

什么ruby指令可以根据action_id提取单个案例?上例中的Signature id: 1, action_id: 1 Signature id: 2, action_id: 2 Signature id: 3, action_id: 1 Signature id: 4, action_id: 3 Signature id: 5, action_id: 2

1 个答案:

答案 0 :(得分:2)

@signatures = Signature.where(action_id: @actions)
                       .group('action_id')
                       .select('id, count(action_id)')
                       .having('count(action_id) = 1')