From 5112cfcb40a188e9586faa246831fba64cb8cbd5 Mon Sep 17 00:00:00 2001 From: Hao Yu Date: Fri, 8 Nov 2024 14:26:07 +1000 Subject: [PATCH] Fixes #37995 - High memory rendering Arf Report index page 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. --- app/controllers/arf_reports_controller.rb | 10 ++++++++-- app/views/arf_reports/_list.html.erb | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/app/controllers/arf_reports_controller.rb b/app/controllers/arf_reports_controller.rb index 65c2d9eb..cfa906eb 100644 --- a/app/controllers/arf_reports_controller.rb +++ b/app/controllers/arf_reports_controller.rb @@ -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 diff --git a/app/views/arf_reports/_list.html.erb b/app/views/arf_reports/_list.html.erb index 94e6ef98..cddf2f29 100644 --- a/app/views/arf_reports/_list.html.erb +++ b/app/views/arf_reports/_list.html.erb @@ -56,4 +56,4 @@ -<%= will_paginate_with_info @arf_reports %> +<%= will_paginate_with_info @arf_reports_pg %>