I refer to this #ruby #csv template at least once a week in my day job. I hope this will also become useful for you!
This template simply loops thru an assumptive group of users in a database. The template can be easily modified for your own use.
Simply modify the desired file path, the objects you would like to loop thru, and execute the following command from the the root directory of your rails app: rails r <name_of_template>
require 'csv'
path = 'file_path.csv';
users = User.order(created_at: :desc);
CSV.open(path, "wb") do |csv|
# headers
csv << ['First Name', 'Last Name', 'Date of Birth'];
# loop thru users
users.each do |u|
csv << [u.first_name, u.last_name, u.date_of_birth];
end
end
Top comments (0)