Export data to CSV file from Rails console

In one of my project I would like to dump and export some information into a csv file. I have produced an easy script to do that. 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 Here is what the above snippet does: Require the CSV library, since we'll be manipulating