title: Rails group by hour of the day for created_at column
body: <p>I have a <code>Subscribe Model</code> and want to group by hour of day. I checked with 
  <a href="https://stackoverflow.com/questions/12657753/rails-group-records-by-dates-of-created-at">rails group records by dates of created_at</a> and <a href="https://stackoverflow.com/questions/10740666/rails-group-by-day-as-well-as-hour">rails - group by day as well as hour</a> But no luck!.</p>
  
  <pre><code>2.3.0 :175 &gt; Subscribe.find 13
    Subscribe Load (0.9ms)  SELECT  "subscribes".* FROM "subscribes" WHERE "subscribes"."id" = $1 LIMIT 1  [["id", 13]]
   =&gt; #&lt;Subscribe id: 13, user_id: 7, publisher_id: 5, created_at: "2015-11-23 00:37:49", updated_at: "2016-03-22 08:04:31", subscription_id: "2"&gt; 
  </code></pre>
  
  <p>on applying the other solution </p>
  
  <pre><code>2.3.0 :174 &gt; Subscribe.where("DATE_PART('hour', created_at) = ?", 13)
    Subscribe Load (1.0ms)  SELECT "subscribes".* FROM "subscribes" WHERE (DATE_PART('hour', created_at) = 13)
   =&gt; #&lt;ActiveRecord::Relation []&gt; 
  </code></pre>
  
  <p>To achieve this I used <a href="https://github.com/vgvinay2/groupdate" rel="nofollow noreferrer">groupdate</a></p>
  
  <pre><code>graph_data =  Subscribe.group_by_hour_of_day(:created_at, format: "%-l %M %P", time_zone: "Kolkata",range: start_date.to_datetime..end_date.to_datetime).count
  
   =&gt; {"12 00 am"=&gt;0, "1 00 am"=&gt;0, "2 00 am"=&gt;0, "3 00 am"=&gt;0, "4 00 am"=&gt;0, "5 00 am"=&gt;1, "6 00 am"=&gt;1, "7 00 am"=&gt;0, "8 00 am"=&gt;0, "9 00 am"=&gt;0, "10 00 am"=&gt;0, "11 00 am"=&gt;0, "12 00 pm"=&gt;1, "1 00 pm"=&gt;1, "2 00 pm"=&gt;0, "3 00 pm"=&gt;0, "4 00 pm"=&gt;0, "5 00 pm"=&gt;0, "6 00 pm"=&gt;0, "7 00 pm"=&gt;0, "8 00 pm"=&gt;0, "9 00 pm"=&gt;0, "10 00 pm"=&gt;0, "11 00 pm"=&gt;0} 
  </code></pre>
  
  <p>but for Subscribe 13 there is an record at created_at (2015-11-23 00:37:49) but it return as 0</p>
  
  <p>Any help would be appreciated!!! </p>