Skip to content

Instantly share code, notes, and snippets.

@mathieugagne
Created November 30, 2017 15:44
Show Gist options
  • Save mathieugagne/3bc61293a7e32b929f86ae9a726faa02 to your computer and use it in GitHub Desktop.
Save mathieugagne/3bc61293a7e32b929f86ae9a726faa02 to your computer and use it in GitHub Desktop.
Distance test to csv
# Setup
question = SurveyQuestion.where(survey_id: 83,
title: 'Distance',
index: 261,
question_type: 'virtual',
type: 'SurveyQuestionDistance',
settings: '{"epicenter_address": "Beauvais, France", "respondent_address_question_id": 7163}').first_or_create
# Add "France" to responses
SurveyResponse.where(survey_question_id: 7163).find_each do |response|
response.update(title: "#{response.title}, France")
end
# Flush cache
ProxyCaching.send(:redis).send(:flushdb)
SurveyHydrateWorker.perform_async(83)
# Gather results
results = {}
SurveyQuestion.find(7163).survey_responses.pluck(:UID, :title).to_h.each do |uid,zip|
results[uid] = {}
results[uid][:zipcode] = zip
end
question.survey_responses.pluck(:UID, :title).to_h.each do |uid, distance|
results[uid] ||= {}
results[uid][:distance] = distance
end
results
# Export
CSV.open("tmp/distances.csv", "wb") do |csv|
csv << ["UID", "Postal Code", "Distance"]
results.each do |uid, result|
csv << [uid, result[:zipcode], result[:distance]]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment