Created
February 6, 2020 21:05
-
-
Save sashadev-sky/4c63649309e9d121570731d9f832cef5 to your computer and use it in GitHub Desktop.
Export data to CSV file from 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
``` | |
require 'csv' | |
file = "#{Rails.root}/public/user_data.csv" | |
products = Product.order(:first_name) | |
headers = ["Product ID", "Name", "Price", "Description"] | |
CSV.open(file, 'w', write_headers: true, headers: headers) do |writer| | |
products.each do |product| | |
writer << [product.id, product.name, product.price, product.description] | |
end | |
end | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment