A quick breakdown of what happens as it helps you understand what to do for your replacement request.
options = {
select: "SUM(1) AS num_demos, product_id ",
group: "product_id",
order: "num_demos ASC",
}
product_ids = Demo.where("state = 'waitlisted'").find(:all, options).collect{|d| d.product_id}
This line will generate
SELECT SUM(1) as num_demos, product_id FROM "demos" WHERE (state = 'waitlisted') GROUP BY product_id
And it returns an array of objects Demosorted by count(*)rows in the group where only the attribute was loaded product_idand is available to you.
Further
sort_product_ids = product_ids.collect{|product_id| "id = #{product_id}"}
results in the product_ids set being displayed in the format "id = x". IE: If the previous result returned 10 results, with product_ids in the range of 1.10, is sort_product_idsnow equivalent["id = 1", "id = 2", "id = 3", "id = 4", "id = 5", "id = 6", "id = 7", "id = 8", "id = 9", "id = 10"]
Finally,
Product.where(visible: true, id: product_ids).order(sort_product_ids.join(', '))
Products, visible true, id product_ids (, , Demo, - ). SQL sort_product_ids ( "id = 1, id = 2, ... id = 10" ["id = 1", "id = 2", ... "id = 10"]).
:
http://guides.rubyonrails.org/active_record_querying.html
http://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html