我有一个范围 -
scope :most_stock, -> {
joins(:stock_keeping_units).
select("items.*, count(stock_keeping_units.id) AS stock_keeping_unit_count").
group('items.id').
order('stock_keeping_unit_count DESC').
where('stock_keeping_units.sold = FALSE')
}
当我运行Item.most_stock
时,一切正常,花花公子。
但是,当我运行Item.most_stock.count
时,我收到以下错误 -
PG::SyntaxError: ERROR: syntax error at or near "AS" LINE 1
生成的SQL查询是 -
SELECT COUNT(items.*, count(stock_keeping_units.id) AS stock_keeping_unit_count) AS count_items_all_count_stock_keeping_units_id_as_stock_keeping_u, items.id AS items_id FROM "items" INNER JOIN "stock_keeping_units" ON "stock_keeping_units"."item_id" = "items"."id" AND "stock_keeping_units"."deleted_at" IS NULL WHERE "items"."deleted_at" IS NULL AND (stock_keeping_units.sold = FALSE) GROUP BY items.id ORDER BY stock_keeping_unit_count DESC
解决方法是使用length
我很想知道count
在这里不起作用或者理想情况下应该有效吗?