Skip to content

Instantly share code, notes, and snippets.

@foxumon
Created April 29, 2016 20:19
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save foxumon/fdb30349545944eee58c7858e6bab23c to your computer and use it in GitHub Desktop.
Save foxumon/fdb30349545944eee58c7858e6bab23c to your computer and use it in GitHub Desktop.
Export table to CSV with all attributes in rails console
require 'csv'
file = "#{Rails.root}/public/data.csv"
table = User.all;0 # ";0" stops output. Change "User" to any model.
CSV.open( file, 'w' ) do |writer|
writer << table.first.attributes.map { |a,v| a }
table.each do |s|
writer << s.attributes.map { |a,v| v }
end
end
@andriibureviy
Copy link

how to connect it to your project?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment