class Admin::LeaderboardController < AdminController
  def index
    counts = Activity.where(actor_type: 'User').
      group(:actor_id).
      order('count_all DESC').
      limit(200).
      count

    @data = counts.map do |user_id, count|
      user = User.find(user_id)

      {
        user: user,
        activity_count: count,
        daily_avg: count / ((Time.now - user.created_at) / 1.day),
        joined_at: user.created_at
      }.with_indifferent_access
    end

    case params[:sort]
    when 'daily_avg'
      @data = @data.sort_by { |x| -x[:daily_avg] }
    when 'joined_at'
      @data = @data.sort_by { |x| x[:joined_at] }
    end

    @data
  end
end


class Admin::LeaderboardController < AdminController
  def index
    sql = "SELECT COUNT(*) AS activity_count, (SELECT users.username FROM users WHERE users.id = activities.actor_id) FROM activities WHERE activities.actor_type = 'User' GROUP BY actor_id ORDER BY activity_count DESC LIMIT 200"
    values = ActiveRecord::Base.connection.execute(sql).values
    @data = values.map do |v|
      { 'user' => user = User.where(username: v.last).first,
        'activity_count' => v.first,
        'daily_avg' => v.first.to_i / ((Time.now - user.created_at) / (60 * 60 * 24.0)),
        'joined_at' => user.created_at}
    end
    case params[:sort]
    when 'daily_avg'
      @data = @data.sort_by{|x| -x['daily_avg']}
    when 'joined_at'
      @data = @data.sort_by{|x| x['joined_at']}
    end
    @data
  end
end


--- 
+++ 
@@ -1,19 +1,29 @@
 class Admin::LeaderboardController < AdminController
   def index
-    sql = "SELECT COUNT(*) AS activity_count, (SELECT users.username FROM users WHERE users.id = activities.actor_id) FROM activities WHERE activities.actor_type = 'User' GROUP BY actor_id ORDER BY activity_count DESC LIMIT 200"
-    values = ActiveRecord::Base.connection.execute(sql).values
-    @data = values.map do |v|
-      { 'user' => user = User.where(username: v.last).first,
-        'activity_count' => v.first,
-        'daily_avg' => v.first.to_i / ((Time.now - user.created_at) / (60 * 60 * 24.0)),
-        'joined_at' => user.created_at}
+    counts = Activity.where(actor_type: 'User').
+      group(:actor_id).
+      order('count_all DESC').
+      limit(200).
+      count
+
+    @data = counts.map do |user_id, count|
+      user = User.find(user_id)
+
+      {
+        user: user,
+        activity_count: count,
+        daily_avg: count / ((Time.now - user.created_at) / 1.day),
+        joined_at: user.created_at
+      }.with_indifferent_access
     end
+
     case params[:sort]
     when 'daily_avg'
-      @data = @data.sort_by{|x| -x['daily_avg']}
+      @data = @data.sort_by { |x| -x[:daily_avg] }
     when 'joined_at'
-      @data = @data.sort_by{|x| x['joined_at']}
+      @data = @data.sort_by { |x| x[:joined_at] }
     end
+
     @data
   end
 end
Swap out the raw SQL for ActiveRecord stuff
