How to group records by day in Rails using Postgres
<p>So I've found these questions:</p>

<ul>
<li><a href="https://stackoverflow.com/questions/29834995/how-to-group-and-count-by--day-in-rails-in-postgres">How to group and count by day in Rails in Postgres?</a></li>
<li><a href="https://stackoverflow.com/questions/902974/grouping-by-week-month-etc-activerecord">Grouping by week/month/etc &amp; ActiveRecord?</a></li>
<li><a href="https://stackoverflow.com/questions/4987392/how-do-i-group-by-day-instead-of-date">How do I group by day instead of date?</a></li>
</ul>

<p>And this gem: <a href="https://github.com/ankane/groupdate" rel="nofollow noreferrer">https://github.com/ankane/groupdate</a></p>

<p>I'm looking to group records by day, in a format like this:</p>

<pre><code>[
  { "2016-03-16" =&gt; [Record1, Record2, Record3] },
  { "2016-03-17" =&gt; [Record1, Record2] },
  { "2016-03-18" =&gt; [Obj1, Obj2] }
]
</code></pre>

<p>I've been able to get this format using this code:</p>

<pre><code>def group_by_criteria
  created_at.to_date.to_s
end

list.group_by(&amp;:group_by_criteria).map {|k,v| { k =&gt; v} }
</code></pre>

<p>However, as explained in other questions, this is not very efficient for a large number of records, I think I should be using grouping from the db, but I'm not sure how to get the format I'm looking for, I tried something like this but I don't get what I'm expecting:</p>

<pre><code>list.order("date_trunc('day', created_at) DESC).map{ |k, v| { k =&gt; v }}
</code></pre>

<p>How can I group records by day from the db like this?</p>