Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A quick breakdown of what's going on, as it'll help you understand what to do for your replacement query.</p> <pre><code>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} </code></pre> <p>This line will generate </p> <pre><code>SELECT SUM(1) as num_demos, product_id FROM "demos" WHERE (state = 'waitlisted') GROUP BY product_id </code></pre> <p>And returns an array of <code>Demo</code> objects, sorted by the <code>count(*)</code> of rows in the group, where only the <code>product_id</code> attribute has been loaded, and is available to you.</p> <p>Next,</p> <pre><code>sort_product_ids = product_ids.collect{|product_id| "id = #{product_id}"} </code></pre> <p>results in a collection of product_ids mapped to the format <code>"id = x"</code>. IE: If the previous result returned 10 results, with product_ids ranging from 1..10, <code>sort_product_ids</code> is now equivalent to <code>["id = 1", "id = 2", "id = 3", "id = 4", "id = 5", "id = 6", "id = 7", "id = 8", "id = 9", "id = 10"]</code></p> <p>Finally,</p> <pre><code>Product.where(visible: true, id: product_ids).order(sort_product_ids.join(', ')) </code></pre> <p>Selects all <code>Products</code> where the column <code>visible</code> is <code>true</code>, and their <code>id</code> is in the array of <code>product_ids</code> (which, as we found out earlier, is actually an array of <code>Demo</code> objects, not integers - this might be causing the query to fail). Then, it asks SQL to sort that result list by the <code>sort_product_ids</code> (sent in as a string <code>"id = 1, id = 2, ... id = 10"</code> instead of an array <code>["id = 1", "id = 2", ... "id = 10"]</code>).</p> <p>More info available at: <a href="http://guides.rubyonrails.org/active_record_querying.html" rel="nofollow">http://guides.rubyonrails.org/active_record_querying.html</a> <a href="http://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html" rel="nofollow">http://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload