title: ActiveRecord: sort of complicated query with nested associations and sorting
body: <p>Alrigth, so I'm not sure if this isn't too specific... But I really don't have a clue how to construct such a query, neither in AR, nor in SQL. So here's the situation:</p>
  
  <ul>
  <li>I have a <code>User</code> model. <code>User</code> has_many <code>Project</code>s. A <code>Project</code>, in turn, have a following associations:</li>
  <li><code>Project</code> has_one <code>BasicEvent</code></li>
  <li><code>Project</code> had_many <code>AdditionalEvent</code>s</li>
  <li><code>BasicEvent</code> and <code>AdditionalEvent</code> classes are built on inheritance from a AR model class, <code>Event</code></li>
  </ul>
  
  <p>Now, the goal is this: on my view, I need:</p>
  
  <ul>
  <li>access to all <code>Project</code>s count per <code>User</code></li>
  <li>access to all <code>Event</code>s count, where <code>happened_at</code> attribute in <code>nil</code></li>
  <li>have all the <code>User</code>s sorted, by the count of <code>Event</code>s, where <code>happened_at</code> attribute in <code>nil</code></li>
  </ul>
  
  <p>I made a couple of attempts at it so far, but didn't really get too far... I will appreciate any help with this complicated (at least from my perspective) query.</p>