Created
July 5, 2021 13:51
-
-
Save cesartalves/c9cd01ff64afd95e12b556bf70471e57 to your computer and use it in GitHub Desktop.
Run SQL query and save to CSV rails
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
results = ActiveRecord::Base.connection.execute(sql) | |
CSV.open('csv.csv', 'w+') do |f| | |
f << results[0].keys | |
results.each do |r| | |
f << r.values | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment