Skip to content

Commit

Permalink
Admin dashboard: do a fancier query to avoid n+1 in region user counts
Browse files Browse the repository at this point in the history
  • Loading branch information
tjgrathwell committed Apr 1, 2017
1 parent d29a664 commit b45d06e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
9 changes: 9 additions & 0 deletions app/controllers/admin_pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ def admin_dashboard
group(:provider).
order('count(*)')

regions_users_count = <<~SQL
SELECT COUNT(*)
FROM regions_users
WHERE region_id = regions.id
SQL
@region_user_counts = Region.
select("name, (#{regions_users_count}) as count").
order('count')

@spammers = User.where(spammer: true)
@spam_events = Event.where(spam: true)
end
Expand Down
6 changes: 3 additions & 3 deletions app/views/admin_pages/admin_dashboard.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@
</tr>
</thead>
<tbody>
<% Region.all.each do |region| %>
<% @region_user_counts.each do |row| %>
<tr>
<td><%= region.name %></td>
<td><%= region.users.count %></td>
<td><%= row.name %></td>
<td><%= row.count %></td>
</tr>
<% end %>
</tbody>
Expand Down

0 comments on commit b45d06e

Please sign in to comment.