I'm facing n+1 issue when using conditions on array that comes from includes query.
Here are the following tables:
rems
|id| |name|
1 aaa
2 bbb
rem_correlatives
|id| |rem_id| |name| |deleted| |pending_tr|
1 1 qqqq1 0 0
2 1 qqqq2 0 1
3 1 qqqq1 1 0
4 1 qqqq2 1 1
5 1 qqqq1 0 0
6 1 qqqq2 0 1
7 1 qqqq1 1 0
8 1 qqqq2 1 1
9 2 qqqq1 0 0
10 2 qqqq2 0 1
11 2 qqqq1 1 0
12 2 qqqq2 1 1
13 2 qqqq1 0 0
14 2 qqqq2 0 1
15 2 qqqq1 1 0
16 2 qqqq2 1 1
Here are the models:
class Rem < ApplicationRecord
has_many :rem_correlatives
end
class RemCorrelative < ApplicationRecord
belongs_to :rem
end
Here is the controller called rems_controller.rb from the location /app/controllers/rems_controller.rb
def index
@list_array = Rem.includes(:rem_correlatives).all
end
Here is the index view from the location /app/views/rems/index.html.erb
<% @list_array.each do |array| %>
<% array.name %>
<% @check_result = array.rem_correlatives.where("deleted=0 AND pending_tr= 1)%>
<% if @check_result.present? %>
No
<% else %>
Yes
<% end %>
<% end %>
<% end%>
I tried this workaround code that display data array using columns pending_tr=1 and deleted=0 but seems not to be a good practice.
<% @list_array.each do |array| %>
<% array.name %>
<% array.rem_correlatives.each do |rem_correlative|%>
<!-- Create condition pending_tr=1 AND deleted=0 -->
<% if rem_correlative.pending_tr == 1 && rem_correlative.deleted == 0%>
<% @check_condition = "No" %>
<% else %>
<% @check_condition = "Yes"%>
<% end %>
<% end %>
<!-- Check results from array if the word yes exists -->
<% if @check_condition.include?("Yes") %>
si
<% else %>
no
<% end %>
<% end%>
Here is the backend result when using the workaround code, its working and not displaying n+1 issue.
How can I prevent n+1 issue from array that is generating additional conditions as good code?

