Skip to content

Commit

Permalink
Fixes #37995 - High memory rendering Arf Report index page
Browse files Browse the repository at this point in the history
Avoid using includes() with nested associations and "order by"
together. Otherwise, includes() will use join tables instead
and Rails somehow create many objects and high memory
consumption.
  • Loading branch information
hao-yu committed Nov 11, 2024
1 parent 244fd5e commit 5112cfc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
10 changes: 8 additions & 2 deletions app/controllers/arf_reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ def model_of_controller
end

def index
# Avoid using includes() with nested associations and "order by" together. Otherwise,
# includes() will use join tables instead and Rails somehow create many objects and
# high memory consumption.
@arf_reports_pg = resource_base.search_for(params[:search], :order => params[:order])
.paginate(:page => params[:page], :per_page => params[:per_page])
arf_report_ids = @arf_reports_pg.pluck(:id)
@arf_reports = resource_base.includes(:policy, :openscap_proxy, :host => %i[policies last_report_object host_statuses])
.search_for(params[:search], :order => params[:order])
.paginate(:page => params[:page], :per_page => params[:per_page])
.where(id: arf_report_ids)
.sort_by{ |arf_report| arf_report_ids.index(arf_report.id) }
end

def show
Expand Down
2 changes: 1 addition & 1 deletion app/views/arf_reports/_list.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@
</div>
</div>
</div>
<%= will_paginate_with_info @arf_reports %>
<%= will_paginate_with_info @arf_reports_pg %>

0 comments on commit 5112cfc

Please sign in to comment.