Skip to content

Instantly share code, notes, and snippets.

@mangantj
Last active October 23, 2017 07:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mangantj/0732375585029a2eb67a to your computer and use it in GitHub Desktop.
Save mangantj/0732375585029a2eb67a to your computer and use it in GitHub Desktop.
Rails mysql to csv with no backing models
ActiveRecord::Base.connection.tables.each{ |table|
headers = ActiveRecord::Base.connection.execute("Select * from #{table} LIMIT 1").fields
CSV.open("#{table}.csv", "wb") do |csv|
csv << headers
ActiveRecord::Base.connection.execute("Select * from #{table}").each{ |row|
csv << row
}
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment