我有产品和库存。 每个储量包含<条码>产品_id、彩色_id、储存_id和_stock。
对于特定产品,我想按储存方式将存货按肤色和产出分类,如:
储存
- 储存
- Color A: in_stock
- Color B: in_stock
- Storage 2:
- Color A: in_stock
- Color B: in_stock
不幸的是,我不能提出看法。
迄今为止,我提出以下问题:
def all_units_in_stock
stocks.in_stock.group(:storage_id, :color_id).includes(:storage, :color)
end
which returns
Product.find(11).all_units_in_stock
Product Load (37.1ms) SELECT "products".* FROM "products" WHERE "products"."id" = ? LIMIT 1 [["id", 11]]
Stock Load (0.3ms) SELECT "stocks".* FROM "stocks" WHERE "stocks"."product_id" = 11 AND (in_stock > 0) GROUP BY storage_id
Storage Load (0.2ms) SELECT "storages".* FROM "storages" WHERE "storages"."id" IN (1, 2, 3, 4, 5)
Color Load (0.2ms) SELECT "colors".* FROM "colors" WHERE "colors"."id" IN (3)
=> [#<Stock id: 162, product_id: 11, storage_id: 1, in_stock: 10.0, created_at: "2011-11-07 22:54:50", updated_at: "2011-11-07 22:54:50", color_id: 3>, #<Stock id: 163, product_id: 11, storage_id: 2, in_stock: 10.0, created_at: "2011-11-07 22:54:50", updated_at: "2011-11-07 22:54:50", color_id: 3>