Skip to content

Instantly share code, notes, and snippets.

View tonatiuh's full-sized avatar

Tonatiuh Núñez tonatiuh

View GitHub Profile
@tonatiuh
tonatiuh / campaign.rb
Created September 22, 2012 01:39
Update another record on after_save on model
after_save :bid_adjusts
def bid_adjusts
unless @old_exclusive_campaign.nil?
campaign = Campaign.find(@old_exclusive_campaign.id)
campaign.update_attributes(exclusivity: false)
campaign.save
end
end
@tonatiuh
tonatiuh / contact.rb
Created September 14, 2012 23:37
Failing test
def new_contact_obligations
# self.vertical = conversions.first.placement.vertical
self.attributes = load_loc_attrs
self.save!
notify_observers(:find_campaigns)
end
@tonatiuh
tonatiuh / gist:3717573
Created September 13, 2012 20:55
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
@tonatiuh
tonatiuh / gist:3710358
Created September 12, 2012 22:16
Records
## The vertical
#Be sure of that vertical with id 1 exists
## The campaign (form)
#Put to the campaign the state of washignton
#Set to the campaign the vertical: 1
## The contacts (on the rails console)
Contact.create(:name => 'Jodee Clore', :vertical_id => 1, :geo_id => '15121') #this geo_id's are citys of washington
Contact.create(:name => 'Trish Roudebush', :vertical_id => 1, :geo_id => '14974')
@tonatiuh
tonatiuh / gist:3670657
Created September 7, 2012 23:22
Error when sending all the vertical form
###app/models/vertical.rb
class Vertical < ActiveRecord::Base
has_one :parent_relation, foreign_key: "child_id", class_name: "VerticalRelation"
has_many :child_relations, foreign_key: "parent_id", class_name: "VerticalRelation"
has_one :head_relation, foreign_key: "tail_id", class_name: "VerticalRelation"
has_one :tail_relation, foreign_key: "head_id", class_name: "VerticalRelation"
has_one :parent, through: :parent_relation
has_many :children, through: :child_relations, source: :child
has_one :head, through: :head_relation