title: rails selecting attributes on includes tables
body: <p>I need to write the following query using rails Active Record:</p>
  
  <pre><code>select c.name,p.name,SUM(e.hours)
  from Events e, Projects p, Unities u, Clients c
  where e.project_id = p.id AND 
        p.unity_id = u.id AND
        u.client_id = c.id
  group by c.name, p.name, e.project_id
  </code></pre>
  
  <p>Models:</p>
  
  <pre><code>class Event &lt; ActiveRecord::Base
    belongs_to :project
  
  class Project &lt; ActiveRecord::Base
    belongs_to :unity
    has_many :events
  end
  
  class Unity &lt; ActiveRecord::Base
    belongs_to :client
    has_many :projects
  end
  
  class Client &lt; ActiveRecord::Base
    has_one :unity
  end
  </code></pre>
  
  <p>I have something like:</p>
  
  <pre><code>Event.includes(project: [unity: :client])
  </code></pre>
  
  <p>But I don't know how to specify the selection of attributes and the group by part.</p>