Skip to content

Instantly share code, notes, and snippets.

@mec
Last active August 29, 2019 11:16
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 mec/90f2d9944fb5a34ddb8d944187a2366d to your computer and use it in GitHub Desktop.
Save mec/90f2d9944fb5a34ddb8d944187a2366d to your computer and use it in GitHub Desktop.
Export objects to csv from the Rails console
require 'csv'
file = "path/to/file"
CSV.open("tmp/schools_mp.csv", "wb") do |csv|
csv << School.attribute_names
School.includes(:local_authority, :local_authority_district).select(&:condition?).each do |school|
csv << school.attributes.values
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment