Skip to content

Instantly share code, notes, and snippets.

@pobing
Created December 24, 2014 07:53
Show Gist options
  • Save pobing/5ab97690230cd564241c to your computer and use it in GitHub Desktop.
Save pobing/5ab97690230cd564241c to your computer and use it in GitHub Desktop.
Ruby query db res and generate csv file
# encoding: utf-8
require 'csv'
desc 'Get all user email'
task :send_coupon do
date = Time.now.strftime("%Y%m%d%H%M%S")
path = "tmp/send_coupon_emails_#{date}.csv"
CSV.open(path, "wb") do |csv|
header = ["email","created_at","survey_count","passport_id"]
csv << header
User.where(:email.ne => nil).each do |u|
csv << [u.email, u.created_at.strftime("%Y-%m-%d"), u.surveys.count, u.passport_id]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment