Skip to content

Instantly share code, notes, and snippets.

@tonatiuh
Created September 13, 2012 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonatiuh/3717573 to your computer and use it in GitHub Desktop.
Save tonatiuh/3717573 to your computer and use it in GitHub Desktop.
method for finding matching geo campaigns
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
@antillas21
Copy link

Not sure though... but I think you'll need to use Campaign.all.find_each instead of Campaign.all.each to make the query memory efficient. See this => http://api.rubyonrails.org/classes/ActiveRecord/Batches.html#method-i-find_each

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment