我有一个Part
对象,它有Supplier
并且部分有 field supplier_id
。
所以我有很多记录,我想确定有多少零件来自多少供应商。所以假设我有这样的部分安排。零件数 = 100
25 came from supplier_id => 3
25 came from supplier_id => 1
50 came from supplier_id => 5
所以这就是我的查询方式:
Part.where(:order_id => 30146).select('count(id) as count, supplier_id').group('supplier_id').order('count DESC')
它似乎也产生了正确的查询:
SELECT count(id) as count, supplier_id FROM "parts" WHERE "parts"."order_id" = 30146 AND ("parts"."deleted_at" IS NULL) GROUP BY supplier_id ORDER BY count DESC
但我的结果并不像我打算的那样:
[#<Part supplier_id: 3>, #<Part supplier_id: 1>, #<Part supplier_id: 5>]
我只得到供应商 ID,但没有得到计数。我想得到这样的输出:
25 => 3
25 => 1
50 => 5
我在这里想念什么?