CSV Ruby Rails Rails 826 February, 2025

Generating and Handling CSV Files in Ruby on Rails 8

Infosia

Nilesh Patil

Founder at infosiaTech

Generate CSV with Ruby on Rails code

Table of Contents

  1. Introduction
  2. Installation CSV Gem
  3. Reading from the file
  4. Writing in the file
  5. Open the file
  6. Close the file

Introduction

Generating a CSV file in the most recent version of Ruby on Rails is essential because most SASS businesses want the most recent data to upload or download in order to obtain the most up-to-date scenario. CSV stands for Comma Seperated Values in Rows. It essentially produces a table structure with a header and values. In csv, each row is separated from previous lines by a new line '\n'. On the row level, each value is separated by ',' (traditionally); we can customize this with an uncommon set of symbols, allowing each value in the row to be separated and distinguished independently.

Installation CSV Gem

To install CSV gem in the system, you need to add gem in the Gemfile.

gem 'csv', '~> 3.0'

Or if you use bash to install csv version.

gem install csv -v 3.0.0

Reference:

Reading from the file

We can directly read each line one by one using foreach method of CSV class, we just need to pass the path of the csv file.

require "csv"
 
CSV.foreach("infosia/exect_folder/smart.csv") do |row|
  # Printing line
  puts row
end

If we want to parse the headers and identify the row value using header values.

require "csv"
 
CSV.foreach("infosia/exect_folder/smart.csv", headers: true) do |row|
  puts row['first_name']
end

Writing in the file

For writing in the file, we need to open the file in write mode, we have to first push the header row then we can loop through the data and push the array into csv file.

require "csv"
 
file_name = "infosiatech.csv"
headers = [ "Email", "First Name", "Last Name", "Name" ]
csv = CSV.open(file_name, "w")
csv << headers
[{"email": 'abc@infosiatech.com', 'first_name': 'ABC', 'last_name': 'XYZ', 'name': 'test'}].each do |contact|
  csv << [ contact["email"], contact["first_name"], contact["last_name"], contact["name"] ]
end
csv.close

Open the file

Using CSV gem, we have to open the file, below systex

# Opens the infosiatech file in write mode.
csv = CSV.open('infosiatech.csv', 'w')

we can use the open method of CSV class, which takes 2 parameters file name and open mode. open mode is utilize to optimize the performance while

Close the file

csv = CSV.open('infosiatech.csv', 'w')
csv.close

to check, it is closed or not, we can use the closed method.

csv.closed?
 
# return true or false

It is important to close the file, because ruby has to free up memory which is consumed by CSV can be large enough to hang the system. It is prescribed to close the file manually, ruby can handle the actions efficiently.

Need help on your Ruby on Rails, React or
Custom Development project?

Book A Call
Infosia

Helping you maximize operations management with digitization

Office

B-401, Sector-4, Moshi, Pune

Contacts

(+91) 810 800 4135

Hello@infosiaTech.com

© 2025. All rights reserved

InfosiaInfosiaInfosiaInfosia