Skip to content

Instantly share code, notes, and snippets.

@milushov
Last active August 29, 2015 14:15
Show Gist options
  • Save milushov/e972a1170d0be20697db to your computer and use it in GitHub Desktop.
Save milushov/e972a1170d0be20697db to your computer and use it in GitHub Desktop.
require 'csv'
options = {
write_headers: true,
headers: ['first_name', 'last_name', 'email', 'phone', 'purchased_courses'],
}
CSV.open("#{Rails.root}/users-for-unisender.csv", 'w', options) do |writer|
User.find_each do |u|
if u.type == 'Student'
courses = u.transactions
.where(payable_type: 'Course', status: 'confirmed')
if courses.any?
coursess_names = courses
.map{ |x| x.payable.title }
.join(', ')
end
writer << [u.name, u.surname, u.email, u.phone, coursess_names]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment