title: How to add virtual fields to query result
body: <p>To improve performance, I have written the following:</p>
  
  <pre><code>a = Address.joins(:vacancies).select('"addresses"."city", COUNT (*) AS total_count').group('"addresses"."city"').order('total_count DESC')
  </code></pre>
  
  <p>instead of:</p>
  
  <pre><code>a = Address.all.keep_if{ |a| a.vacancies.all_active.size &gt; 0 }.uniq{ |a| a.city }.map{ |a| [a.city, a.vacancies.all_active.size] }.sort{ |aa, ab| aa[1] &lt;=&gt; ab[1] }.reverse
  </code></pre>
  
  <p>However, the <code>total_count</code> field doesn't show up in the result. If I run the query in sqlite it DOES show up. I added a virtual attribute to the Address model but this doesn't help. How to fix this?</p>
  
  <p>In addition, this is my Rails console output:</p>
  
  <pre><code>1.9.3p194 :010 &gt; a = Address.joins(:vacancies).select("addresses.city, count(*) as total_count").group("addresses.city").order("total_count DESC")
    Address Load (3.9ms)  SELECT addresses.city, count(*) as total_count FROM "addresses" INNER JOIN "vacancies" ON "vacancies"."address_id" = "addresses"."id" GROUP BY addresses.city ORDER BY total_count DESC
   =&gt; [#&lt;Address city: "Amsterdam"&gt;, #&lt;Address city: "Rotterdam"&gt;, #&lt;Address city: "Utrecht"&gt;, #&lt;Address city: "Den haag"&gt;, #&lt;Address city: "Delft"&gt;, #&lt;Address city: "Hilversum"&gt;, #&lt;Address city: "Amstelveen"&gt;, #&lt;Address city: "Rijswijk"&gt;, #&lt;Address city: "Breda"&gt;, #&lt;Address city: "Zwolle"&gt;, #&lt;Address city: "Tilburg"&gt;, #&lt;Address city: "Amersfoort"&gt;, #&lt;Address city: "Amsterdam centrum"&gt;, #&lt;Address city: "Eindhoven"&gt;, #&lt;Address city: "Next nature netwerk, amsterdam"&gt;, #&lt;Address city: "Almere"&gt;, #&lt;Address city: "Ameide"&gt;, #&lt;Address city: "Arnhem"&gt;, #&lt;Address city: "Baarlo"&gt;, #&lt;Address city: "Capelle a/d ijssel"&gt;, #&lt;Address city: "De lier"&gt;, #&lt;Address city: "Leiden"&gt;, #&lt;Address city: "Reigerstraat 16, breda"&gt;, #&lt;Address city: "Rottedam"&gt;, #&lt;Address city: "'s-hertogenbosch "&gt;, #&lt;Address city: "Alkmaar"&gt;, #&lt;Address city: "Alphen aan den rijn"&gt;, #&lt;Address city: "Amsterdam singel 137"&gt;, #&lt;Address city: "Amsterdam, keizergracht"&gt;, #&lt;Address city: "Barendrecht"&gt;, #&lt;Address city: "Den haag e.o."&gt;, #&lt;Address city: "Deventer"&gt;, #&lt;Address city: "Dordrecht"&gt;, #&lt;Address city: "Groningen"&gt;, #&lt;Address city: "Gummo, amsterdam"&gt;, #&lt;Address city: "Hillversum"&gt;, #&lt;Address city: "Houten"&gt;, #&lt;Address city: "Lisse"&gt;, #&lt;Address city: "Moti, breda"&gt;, #&lt;Address city: "Nijmegen"&gt;, #&lt;Address city: "Rotterdam "&gt;, #&lt;Address city: "Tuil (waardenburg)"&gt;, #&lt;Address city: "Zaltbommel"&gt;] 
  1.9.3p194 :011 &gt; a.first.total_count
   =&gt; nil 
  1.9.3p194 :012 &gt; a.first.to_json
   =&gt; "{\"city\":\"Amsterdam\",\"total_count\":null}"
  </code></pre>