title: Select from database a specific JSON node using Rails/AREL?
body: <p>Pretend I have a table called <code>books</code> and it has a <code>json</code> type column called <code>misc</code> where some miscellaneous data has been saved. Using Rails/AREL I would like to extract just <code>id</code> and the number of pages (<code>num_pages</code>) from all the books.</p>
  
  <p>In <code>psql</code> I can do the following:</p>
  
  <pre><code>SELECT id, misc-&gt;'num_pages' FROM books ;
    id   |   ?column?    
  -------+---------------
   23562 | "220"
   22188 | "355"
  </code></pre>
  
  <p>(Note: the above query produces the same results with <code>--&gt;</code> as with <code>-&gt;</code>).</p>
  
  <p>I expected this to work:</p>
  
  <pre><code>pry&gt; Book.select("id, misc-&gt;&gt;'num_pages'")
    Book Load(1.7ms)  SELECT  id, etc--&gt;'num_pages' FROM "books"
    Book Load(1.5ms)  SELECT  id, etc--&gt;'num_pages' FROM "books"
  =&gt; #&lt;Book::ActiveRecord_Relation:0x3ff7e934d8f4&gt;
  
  pry&gt; Book.select("id, misc-&gt;'num_pages'")
    Book Load(1.1ms)  SELECT  id, etc-&gt;'num_pages' FROM "books"
  =&gt; [#&lt;Book:0x00007fefd3c11a68 id: 23562&gt;, #&lt;Book:0x00007fefd3c116a8 id: 22188&gt;]
  </code></pre>
  
  <p>...when I remove the <code>-&gt;'num_pages'</code> part, it returns the entire <code>misc</code> field (so I know it's <em>almost</em> working):</p>
  
  <pre><code>pry&gt; Book.select("id, misc")
    Book Load(0.9ms)  SELECT  id, etc FROM "books"
  =&gt; [#&lt;Book:0x00007fefe4e99fb8
    id: 23562,
    etc:
     {"num_pages"=&gt;"220",
  ...
  </code></pre>