I know, it looks awful but it worked for a while. But now the number of users is so large that the system started to “forget” where exactly it was (losing connection to database)
What’s the best practice to rewrite this nested loop?
User.all.each_with_index do
get Subset of criteria to select data
Subset1.each do
getSubset2
Subset2.each do
getSubset3
Subset3.each do
getSubset4
Subset4.each do
compute something
open file A
create or update a line
end
end
end
end
end
end
edit:
Subsets are either queries or predefined arrays. I am trying to combine it as suggested, will brb
User.all.each_with_index do |user|
Subset1.each do |parameter1|
Subset2(function(user,parameter1)).each do |object2|
Subset3.each do |parameter3|
getSubset4(user, parameter1, object2, parameter3)
Subset4.each do |data|
p data
end
end
end
end
end
Piggy backing off of @steenslag’s answer, let’s say your users are
Rather than loop through all users, then all users of a city, then all users of a gender, break those nested queries out.
Now you can iterate through one array (criteria) that collects the cross product of all those arrays.