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)
Here it is. It runs super fast now :