Skip to content

Commit

Permalink
Get rid of N+1s
Browse files Browse the repository at this point in the history
  • Loading branch information
luizkowalski committed Apr 8, 2024
1 parent 2f3a094 commit f1d16ef
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
4 changes: 3 additions & 1 deletion app/controllers/solid_errors/errors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ def index

@errors = Error.unresolved
.joins(:occurrences)
.select(errors_table[Arel.star], occurrences_table[:created_at].maximum.as("recent_occurrence"))
.select(errors_table[Arel.star],
occurrences_table[:created_at].maximum.as("recent_occurrence"),
occurrences_table[:id].count.as("occurrences_count"))
.group(errors_table[:id])
.order(recent_occurrence: :desc)
end
Expand Down
4 changes: 2 additions & 2 deletions app/views/solid_errors/errors/_row.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
<pre class="whitespace-pre-wrap ml-6 mt-4"><%= error.message %></pre>
</td>
<td scope="col" class="whitespace-nowrap px-3 py-4 pt-7 text-gray-500 text-right">
<%= error.occurrences.size %>
<%= error.occurrences_count %>
</td>
<td scope="col" class="whitespace-nowrap px-3 py-4 pt-7 text-gray-500 text-right">
<% last_seen_at = error.occurrences.maximum(:created_at) %>
<% last_seen_at = error.recent_occurrence %>
<abbr title="<%= last_seen_at.iso8601 %>" class="cursor-help">
<%= time_tag last_seen_at, time_ago_in_words(last_seen_at, scope: 'datetime.distance_in_words.short') %>
</abbr>
Expand Down

0 comments on commit f1d16ef

Please sign in to comment.