title: Grouping by attribute of associated objects
body: <pre><code>class Event &lt; ActiveRecord::Base
    has_many :meetings
  end
  
  class Meeting &lt; ActiveRecord::Base
    belongs_to :event
  end
  </code></pre>
  
  <p>How to write mysql query to search all events <code>group_by</code> meeting <code>DATE(start_at)</code>? </p>
  
  <pre><code>Event.inludes(:meetings).group ...
  </code></pre>
  
  <p>As a result I want to get a <code>Hash</code>:</p>
  
  <pre><code>{"2014-01-24"=&gt;[#&lt;Event id: , title: "First", created_at: "2014-01-24 16:02:52", updated_at: "2014-01-24 16:02:52"&gt;, #&lt;Event id: 2, title: "Second", created_at: "2014-01-24 16:02:52", updated_at: "2014-01-24 16:02:52"&gt;], "2013-01-29"=&gt;[#&lt;Event id: 3, title: "Third", created_at: "2013-01-29 05:30:40", updated_at: "2014-01-29 05:30:40"&gt;], ...]}
  </code></pre>
  
  <p>P.S: I am using <code>PostgreSQL</code></p>
  
  <p>Now I get it by this way:</p>
  
  <pre><code>hash = {}
  Meeting.where("extract(month from start_at) = ?", Date.today.month).pluck('DATE(start_at)').uniq.each do |date| 
    hash[date] = Event.includes(:meetings).where("DATE(meetings.start_at) = ?", date).references(:meetings)
  end
  </code></pre>
  
  <p>But it produced so many queries to the database :(</p>