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 4234672
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:23:18+00:00 2026-05-21T02:23:18+00:00

I am trying to cycle through a few of these blocks. They basically narrow

  • 0

I am trying to cycle through a few of these blocks. They basically narrow down a number of people that fulfill a bunch of attributes.

I apologize if this seems really messy, but my database is really taking a toll processing this, and I know there’s a better way. I’m just lost on strategy right now.

My Code:

def count_of_distribution

#beginning with an array..
array_of_users = []

# any matching zip codes? ..
# zip_codes
@zip_codes = self.distributions.map(&:zip_code).compact
unless @zip_codes.nil? || @zip_codes.empty? 
  @matched_zips = CardSignup.all.map(&:zip_code) & @zip_codes
  @matched_zips.each do |mz| 
    CardSignup.find(:all, :conditions => ["zip_code = ?", mz]).each do |cs|
     array_of_users << cs.id
    end
  end
end

# any matching interests?..
# interest
@topics = self.distributions.map(&:me_topic).compact
unless  @topics.nil? || @topics.empty?
  @matched_topics = MeTopic.all.map(&:name) & @topics
  @matched_topics.each do |mt|
    MeTopic.find(:all, :conditions => ["name = ?", mt]).each do |mt2|
      mt2.users.each do |u|
        array_of_users << u.card_signup.id if u.card_signup
      end
    end
  end
end

# any matching sexes?..
# sex
@sexes = self.distributions.map(&:sex).compact
unless @sexes.nil? || @sexes.empty?
  @matched_sexes = CardSignup.all.map(&:sex) & @sexes
  @matched_sexes.each do |ms|
    CardSignup.find(:all, :conditions => ["sex = ?", ms]).each do |cs|
      array_of_users << cs.id
    end
  end
end

  total_number = array_of_users.compact.uniq

  return total_number
end

This is the most embarressing results ever :

Completed in 51801ms (View: 43903, DB: 7623) | 200 OK [http://localhost/admin/emails/3/distributions/new]

UPDATED ANSWER It is truncated but still takes a huge toll on the DB

  array_of_users = []

  @zip_codes = self.distributions.map(&:zip_code).compact
  @sexes = self.distributions.map(&:sex).compact

  @zips_and_sexes = CardSignup.find(:all, :conditions => ["gender IN (?) OR zip_code IN (?)", my_sexes, my_zips])
  @zips_and_sexes.each{|cs| array_of_users << cs.id }

  @topics = self.distributions.map(&:me_topic).compact
  @all_topics = MeTopic.find(:all, :conditions => ["name IN (?)", @topics])
  array_of_users << CardSignup.find(:all, :conditions => ["user_id IN (?)", @all_topics.map(&:users)]).map(&:id)
  • 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-05-21T02:23:19+00:00Added an answer on May 21, 2026 at 2:23 am

    Here it is. It runs super fast now :

    array_of_users = []
    
    # zips and sexes
    @zip_codes = self.distributions.map(&:zip_code).compact
    @sexes = self.distributions.map(&:sex).compact
    @zips_and_sexes = CardSignup.find(:all, :conditions => ["gender IN (?) OR zip_code IN (?)", @sexes, @zip_codes])
    @zips_and_sexes.each{|cs| array_of_users << cs.id }
    
    # interest
    @topics = self.distributions.map(&:me_topic).compact
    @selected_topics = MeTopic.find(:all, :conditions => ["name in (?)", @topics]).map(&:id)
    @matched_users = ActiveRecord::Base.connection.execute("SELECT * FROM `me_topics_users` WHERE (me_topic_id IN ('#{@selected_topics.join("', '")}') )")
    @list_of_user_ids = []
    @matched_users.each{|a| @list_of_user_ids << a[0] }
    @list_of_user_ids.uniq!
    array_of_users << CardSignup.find(:all, :conditions => ["user_id IN (?)", @list_of_user_ids]).map(&:id)
    
    # age
    @ages = self.distributions.map(&:age).compact
    @ages_array = []
    @ages.each{|a| @ages_array << how_old(a) }
    @ages_array.each{|aa| array_of_users << aa.id}
    
    array_of_users << CardSignup.all.map(&:id) if array_of_users.flatten.empty?
    total_number = array_of_users.flatten.uniq
    
    return total_number
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to cycle through a few divs that are generated dynamically through wordpress.
I am trying to write a loop that will cycle through colors and condense
I'm trying to cycle through the controls on a page that consists of textboxes
I'm trying to create a custom Jinja variable that will cycle through a list
I'm trying to cycle through some table rows. The simplified rows are as follows:
I'm trying to cycle through the data contained in each node in a list
I am trying to cycle through the results of my query to display in
I'm trying to cycle through my list itemRenderers to change the data of a
I have a group of section tags which I am trying to cycle through
I'm trying to cycle backwards through an array by clicking a button. My current

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.