Created
February 1, 2013 22:05
-
-
Save ewpeters/4694475 to your computer and use it in GitHub Desktop.
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
members = MemberVisitor.find(:all, :conditions => "destination_id is not Null AND pending_destination_id is null and destinations.ANI like 'SNAP%'", :include => :destination); nil; | |
def pick_best_or_oldest(memv, status) | |
memv_dests = memv.member_visitor_destinations.find(:all, :conditions => "validate_status = '#{status}' and destinations.ANI not like 'SNAP%'", :include => :destination, :order => "created_at asc") | |
if memv_dests.size > 1 | |
snap_ani = memv.snap_phone_number_ani | |
if snap_ani | |
best_memv_dest = memv.member_visitor_destinations.find(:first, :conditions => "validate_status = '#{status}' and destinations.ANI = '#{snap_ani}'", :include => :destination) | |
end | |
end | |
best_memv_dest ||= memv_dests.first | |
end | |
members.each do |memv| | |
ActiveRecord::Base.transaction do | |
begin | |
validated = pick_best_or_oldest(memv, 'validated') | |
if validated | |
memv.pending_destination_id = validated.destination_id | |
memv.save | |
memv.merge_pending_destination! | |
else | |
pending = pick_best_or_oldest(memv, 'pending') | |
if pending | |
memv.pending_destination_id = pending.destination_id | |
memv.save | |
else | |
puts "We have a lost little child: #{memv.id}" | |
end | |
end | |
rescue | |
puts "Problem with #{memv.id} rolling back" | |
raise ActiveRecord::Rollback | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment