title: Rails + ChartKick Pie Chart Counting
body: <p>I've got a Rails app with two models and a relationship between them. I am using ChartKick to display the number of Provider Orders by Zipcode.</p>
  
  <p><strong>order.rb Model</strong></p>
  
  <pre><code>class Order &lt; ActiveRecord::Base
  belongs_to :user
  belongs_to :provider
  belongs_to :address
  </code></pre>
  
  <p><strong>provider.rb Model</strong></p>
  
  <pre><code>class Provider &lt; ActiveRecord::Base
    belongs_to :user
    has_many :orders
  
  def past_orders_chart
      Order.where(:provider_id =&gt; self.id).where(:status =&gt; "2")
  end
  </code></pre>
  
  <p><strong>address.rb model</strong></p>
  
  <pre><code>class Address &lt; ActiveRecord::Base
     belongs_to :user
     has_many :orders 
  </code></pre>
  
  <p><strong>past_orders.html.erb</strong></p>
  
  <pre><code>&lt;%= pie_chart @past_orders_chart.includes(:address).group(:zipcode).sum(:zipcode) %&gt;
  </code></pre>
  
  <p><a href="http://i.stack.imgur.com/Kg5ve.png" rel="nofollow">Here is how the chart looks using 'sum':</a></p>
  
  <p>The problem is it is summing the zipcodes which makes sense based on the above command.   </p>
  
  <p>If I change the command to 'count' instead of 'sum', it displays the graph but only counts the unique occurrences of the zipcode.</p>
  
  <pre><code> pie_chart  =@past_orders_chart.includes(:address).group(:zipcode).count(:zipcode)
  </code></pre>
  
  <p><a href="http://i.stack.imgur.com/psNRn.png" rel="nofollow">Here is how the chart looks using 'count':</a></p>
  
  <p>Using 'sum' produces the correct pie chart visually (in terms of percentages shown in the chart.</p>
  
  <p>So the question is how can I get the chart to list the total number of times the zipcode appears in the dataset? <em>I tried to post a third example screenshot but was blocked by SO.</em></p>
  
  <p>Thanks,</p>
  
  <p>David</p>