Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8062455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:40:28+00:00 2026-06-05T10:40:28+00:00

I know, it looks awful but it worked for a while. But now the

  • 0

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 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T10:40:29+00:00Added an answer on June 5, 2026 at 10:40 am

    Piggy backing off of @steenslag’s answer, let’s say your users are

    #name, gender, city, profession
    Alice, female, Los Angeles, doctor
    Bob, male, Los Angeles, lawyer
    Carol, female, New York, astronaut
    David, male, New York, programmer
    

    Rather than loop through all users, then all users of a city, then all users of a gender, break those nested queries out.

    #bad
    User.each do |u1|
      u1.cities.each do |u2|
        u2.genders.each do |u3|
          u3.professions.each do |u4|
            u4.some_method
          end
        end
      end
    end
    
    #better
    cities = ["Los Angeles", "New York"]
    genders = ["female", "male"]
    professions = ["lawyer", "doctor", "astronaut", "programmer"]
    criteria = cities.product(genders, professions)
    

    Now you can iterate through one array (criteria) that collects the cross product of all those arrays.

    criteria.each do |cr|
      city, gender, profession = cr
      u = User.find_by_city_and_gender_and_profession(city, gender, profession)
      u.some_method
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know it looks not necessary but I hope that it would help me
I know this looks embarrassingly easy, and I guess the problem is that I
This looks like a long shot, but does anyone know of a way to:
My local airport disgracefully blocks users without IE, and looks awful. I want to
I know this looks similar to other questions here but I don't think my
I know this looks like a silly question, but using object oriented stuff with
I know it looks like a silly question , but how am I supposed
I've got a simple question that I just KNOW won't be possible, but here
I know this looks a bit long but i tried to explain the problem
This jQuery isn't working. I know it looks kind of odd. But how can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.