title: Rails: How to sum and display total quantity by category
body: <p>I'd like to sum total quantities by category.</p>
  
  <p>How can I calculate and display them?</p>
  
  <p>The display image is as followings;
  (item A and C are the same category)</p>
  
  <pre><code>order A
    shop A
  
      name: item A
      category: 1
      quantity: 2
  
      name: item B
      category: 2
      quantity: 3
  
      name: item C
      category: 1
      quantity: 4
  
      &lt;Total&gt;     # I'd like to know how to calculate and display
      category 1: 6
      category 2: 3
  </code></pre>
  
  <p>So it would be appreciated if you could give me any advice.</p>
  
  <p><strong>models</strong></p>
  
  <pre><code>class Order &lt; ActiveRecord::Base
      has_many :shops
  end
  
  class Shop &lt; ActiveRecord::Base
      belongs_to :order
      has_many :items
  end
  
  class Item &lt; ActiveRecord::Base
      belongs_to :shop
      has_one :order, autosave: false, through: :shop
  end
  </code></pre>
  
  <p><strong>schema.rb</strong></p>
  
  <pre><code>ActiveRecord::Schema.define(version: 20160610051929) do
  
    create_table "items", force: :cascade do |t|
      t.string   "name"
      t.integer  "category"
      t.integer  "quantity"
      t.integer  "shop_id"
      t.datetime "created_at", null: false
      t.datetime "updated_at", null: false
    end
  
    create_table "orders", force: :cascade do |t|
      t.string   "name"
      t.datetime "created_at", null: false
      t.datetime "updated_at", null: false
    end
  
    create_table "shops", force: :cascade do |t|
      t.string   "name"
      t.integer  "order_id"
      t.datetime "created_at", null: false
      t.datetime "updated_at", null: false
    end
  
  end
  </code></pre>
  
  <p><strong>orders_controller.rb</strong></p>
  
  <pre><code>class OrdersController &lt; ApplicationController
    def show
      @orders = Order.find(params[:id])
    end
  end
  </code></pre>
  
  <p><strong>view/orders/show.html.erb</strong></p>
  
  <pre><code>&lt;%= render @orders %&gt;
  </code></pre>
  
  <p><strong>view/orders/ _order.html.erb</strong></p>
  
  <pre><code>&lt;%= order.name %&gt;&lt;br&gt;
  
  &lt;% order.shops.each do |shop| %&gt;
  
      &lt;%= shop.name %&gt;&lt;br&gt;
  
      &lt;% shop.items.each do |item| %&gt;
  
          name: &lt;%= item.name %&gt;&lt;br&gt;
          category: &lt;%= item.category %&gt;&lt;br&gt;
          quantity: &lt;%= item.quantity %&gt;&lt;br&gt;
          &lt;br&gt;
  
      &lt;% end %&gt;
  
  &lt;% end %&gt;
  </code></pre>
  
  <p>It would be appreciated if you could give me how to calculate and display in total.</p>