title: Group by name, count and total, and display it in a view using Rails
body: <p>I thought I wasn't a rookie anymore, until I stumbled upon this.</p>
  
  <p>I'm trying to load an html list, grouping shops per user and displaying per shop, the number of receipts and total amount.</p>
  
  <p>In Sql I would do this easily with a group by, so I was trying to load the code below, within my Shop Model, into my Rails Console.</p>
  
  <pre><code> def self.group_receipts(user, search_hash=nil)
  
      shop_ids = Shop.pluck(:id) &amp; Receipt.from_companies(user, search_hash).pluck(:shop_id)
  
      #greceipt means Grouped Receipt
      greceipt = Struct.new(:name, :number_of_receipts, :total)
  
      query = Shop.joins(:receipts).where('shops.id in (?)',shop_ids).select('shops.name,count(receipts.id) as number_of_receipts,sum(receipts.total) as total').group('shops.id')
  
      query
  
    end
  </code></pre>
  
  <p>Here is my output</p>
  
  <pre><code>&gt;&gt; Shop.group_receipts(302).all
     (2.0ms)  SELECT id FROM "shops" 
     (3.0ms)  SELECT shop_id FROM "receipts" WHERE (receipts.id IN (SELECT receipts.id
   FROM receipts
   INNER JOIN shops on shops.id=receipts.shop_id
   INNER JOIN companies on companies.id = shops.company_id
   INNER JOIN user_companies on user_companies.company_id = companies.id
   WHERE user_companies.user_id = 302)) AND (receipts.is_manual is null or receipts.is_manual=false) ORDER BY receipts.created_at DESC
    Shop Load (2.0ms)  SELECT shops.name,count(receipts.id) as number_of_receipts,sum(receipts.total) as total FROM "shops" INNER JOIN "receipts" ON "receipts"."shop_id" = "shops"."id" WHERE (shops.id in (16)) GROUP BY shops.id
  [#&lt;Shop name: "Kirlin Osinski and Dooley"&gt;]
  </code></pre>
  
  <p>If my query seems to be all right, why is that that my output is not something like <code>name, 10, 1000</code> ?</p>
  
  <p>That greceipt struct you find on the method definition intends to create a structure so that, we can later access gs.name, gs.number_of_receipts, gs.total, but i cannot understand how to load a list of objects of this struct type from the output presented above :-S.</p>
  
  <p>Anyone to the rescue?</p>