ruby on rails - Using `count` with scopes that have a select method -
i have scope --
scope :most_stock, -> { joins(:stock_keeping_units). select("items.*, count(stock_keeping_units.id) stock_keeping_unit_count"). group('items.id'). order('stock_keeping_unit_count desc'). where('stock_keeping_units.sold = false') } when run item.most_stock, works fine , dandy.
however, when run item.most_stock.count, following error -
pg::syntaxerror: error: syntax error @ or near "as" line 1
the generated sql query -
select count(items.*, count(stock_keeping_units.id) stock_keeping_unit_count) count_items_all_count_stock_keeping_units_id_as_stock_keeping_u, items.id items_id "items" inner join "stock_keeping_units" on "stock_keeping_units"."item_id" = "items"."id" , "stock_keeping_units"."deleted_at" null "items"."deleted_at" null , (stock_keeping_units.sold = false) group items.id order stock_keeping_unit_count desc
a work around use length
i'm curious know count didn't work here or should ideally work?
Comments
Post a Comment