Created
March 3, 2017 20:53
-
-
Save foxumon/4f58019ed4f83b28983eda9565c7fac4 to your computer and use it in GitHub Desktop.
Export table to CSV with select attributes in rails console
This file contains hidden or 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
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