Created
September 13, 2012 20:55
-
-
Save tonatiuh/3717573 to your computer and use it in GitHub Desktop.
method for finding matching geo campaigns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def self.find_matching_campaigns_by_geo(contact) | |
matching_campaigns = [] | |
Campaign.all.each do |campaign| | |
campaign.geos.each do |geo| | |
match = Geo.where(id: contact.geo.id).includes(:geo_polygons).joins("LEFT JOIN geo_polygons as container ON container.geo_id = #{geo.id}").where('ST_Within(geo_polygons.poly, container.poly)') | |
matching_campaigns << campaign unless match.empty? | |
end | |
end | |
matching_campaigns | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not sure though... but I think you'll need to use
Campaign.all.find_each
instead ofCampaign.all.each
to make the query memory efficient. See this => http://api.rubyonrails.org/classes/ActiveRecord/Batches.html#method-i-find_each