How to Seed a Rails Database with a CSV file

CSV files can be overwhelming, but it’s a common way to store large amounts of data. Here’s a walkthrough on how to seed it into your Rails database

Warren Niu
4 min readJul 10, 2021

Y’all should’ve seen my face when I downloaded my first CSV file.

What the hell is this?

First of all, I had never seen anything like it before— it was super jumbled up.

I didn’t even know what a CSV file is, let alone how to use it. I had my work cut out for me.

After deciding to build a Rails backend, I had to somehow figure out how to bring that CSV file into my database. I found a wonderful walkthrough on GitHub Gist that was exactly what I needed & I wanted to write it here in case you missed it:

Source: https://gist.github.com/arjunvenkat/1115bc41bf395a162084

What is a CSV File — An Example

CSV stands for comma separated values, which is a simple text file in which information is separated by commas. They are most commonly encountered in spreadsheets and databases and can be used to move data between programs that aren’t ordinarily able to exchange data.

Let’s look at a simple example that involves some baseball players:

In many cases, CSV files come with headers, and we see that here in the first paragraph.

Then we have the data, which, as the name states, are separated by commas and listed in the same order as our header values.

So we have this file — let’s see how we can seed our database with the same data & use it in our application.

Step 1: Setup

Create a table with columns that appropriately match your seed data. The names doesn’t exactly have to match up. In my example, I’ve named my columns player_id, last_name, and first_name.

Let’s setup our file structure next.

First, create a folder inside of lib called seeds.

Inside our newly created seeds folder, drag & drop your CSV file. In the example I will be using, the file is called players.csv

Step 2: Read the CSV File

In our seeds.rb file, put in the following lines (replacing players.csv with the name of your own CSV file):

In our top line, we’re requiring the Ruby CSV library so we can parse our CSV data.

The next line reads the CSV file into a variable (which we’re calling it csv_text). The last line prints the contents of the variable. When you run rails db:seedyou should see a wall of text representing your CSV data.

We’ll keep building off this code until we’ve created a working seeds file. You should be able to run rails db:seedat the end of each step to make sure everything is working properly after each step!

Step 3: Parse the CSV Data

Let’s add this next line to our code. This new line converts our CSV file into something that Ruby can read. Save this line to a new variable (I called it csv).

The :headers => true option tells the parser to ignore the first line of the CSV file. If your CSV file has no headers, then set it to false.

Step 4: Loop through Parsed data and Create a Database Object from each row

Our final steps will be to loop through the entire CSV file and convert each row of our file into a hash. If we have headers, the header values will be used as keys of the hash.

Make sure to set your values to the exact name of your headers in your CSV file!

Conclusion

Hope this walkthrough helps! Again, the source of my article came from the wonderful GitHub Gist written by arjunvenkat. Definitely check it out in case you have further questions.

With our databases now seeded with our proper data, our apps are now able to be connected and we have the potential to complete some powerful features utilizing our data.

Until next time!

--

--

Warren Niu

Uncovering the truths of Software Engineering one story at a time. Former Healthcare Administrator and proud dad of my Pomeranian, Nami. Based in Brooklyn, NY