我有一個Part
對象,它有Supplier
和部分有字段supplier_id
。ActiveRecord計數和組聲明
所以我有一些記錄,我想確定有多少部分來自多少供應商。所以我們假設我有這樣的部件。零件數= 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
缺少什麼我在這裏?
你可以顯示剩餘的代碼,它產生你看到的輸出嗎? –