title: Building a subquery with ARel in Rails3
body: <p>I am trying to build this query in ARel:</p>
  
  <pre><code>SELECT FLOOR(AVG(num)) FROM (
  SELECT COUNT(attendees.id) AS num, meetings.club_id FROM `meetings` INNER JOIN `attendees` ON `attendees`.`meeting_id` = `meetings`.`id` WHERE (`meetings`.club_id = 1) GROUP BY meetings.id) tmp
  GROUP BY tmp.club_id
  </code></pre>
  
  <p>It returns the average number of attendees per meeting, per club. (a club has many meetings and a meeting has many attendees)</p>
  
  <p>So far I have (declared in class Club &lt; ActiveRecord::Base):</p>
  
  <pre><code>num_attendees = meetings.select("COUNT(attendees.id) AS num").joins(:attendees).group('meetings.id')
  Arel::Table.new('tmp', self.class.arel_engine).from(num_attendees).project('FLOOR(AVG(num))').group('tmp.club_id').to_sql
  </code></pre>
  
  <p>but, I am getting the error:</p>
  
  <pre><code>undefined method `visit_ActiveRecord_Relation' for #&lt;Arel::Visitors::MySQL:0x9b42180&gt;
  </code></pre>
  
  <p>The documentation for generating non trivial ARel queries is a bit hard to come by. I have been using <a href="http://rdoc.info/github/rails/arel/master/frames" rel="nofollow">http://rdoc.info/github/rails/arel/master/frames</a> Am I approaching this incorrectly? Or am I a few methods away from a solution?</p>