title: How can I get this complicated query to work with ActiveRecord?
body: <p>I have a complicated SQL query involving 3 models, creating virtual attributes based off of time operations...  Ideally I would want to use ActiveRecord arel methods to generate this so that it's nicer, but I have no idea how.</p>
  
  <pre><code>  find_by_sql &lt;&lt;--sql
      SELECT username, count(*) as records, AVG(time_taken) as time_taken
      FROM (
        SELECT TIMESTAMPDIFF(HOUR, posts.created_at, reports.created_at) as time_taken,
               users.username as username
        FROM reports, users, posts
        WHERE reports.user_id = users.id AND 
              reports.post_id = posts.id
      ) AS dashboard_data # this name is unused, but apparently required
      GROUP BY username
    sql
  </code></pre>
  
  <p>Is there any way to do something like this within ActiveRecord?  Or is raw sql the only way to do complicated stuff like this?</p>