Skip to content

Instantly share code, notes, and snippets.

@foxumon
Created March 3, 2017 20:53
Show Gist options
  • Save foxumon/4f58019ed4f83b28983eda9565c7fac4 to your computer and use it in GitHub Desktop.
Save foxumon/4f58019ed4f83b28983eda9565c7fac4 to your computer and use it in GitHub Desktop.
Export table to CSV with select attributes in rails console
date = Date.new(2016,12,15)
l = Table.where("created_at > ? AND column != 'variable'", date)
ActiveRecord::Base.logger = nil
results = []
l.each do |i|
u = User.where(:email => i.email)
if u.present?
if Table.where(:email => u[0].email).blank?
results << {:email=>i.email,:member=>'y'}
end
else
results << {:email=>i.email,:member=>'n'}
end
end;0
require 'csv'
file = "#{Rails.root}/public/data.csv"
CSV.open( file, 'w' ) do |writer|
writer << results.first.map { |a,v| a }
results.each do |s|
writer << s.map { |a,v| v }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment