[RUBY] Script to make yaml from CSV to put initial data in Rails with Fixtures

It is well known that the method of inputting data to the Rails database is to create a yaml file for fixtures and input it with the rails command. However, in reality, it seems that the original data is often prepared in Excel etc. and handled in CSV etc. I also tried yaml converter etc. but it didn't work to put the fixtures key, so I made it myself. I haven't done anything like processing escape characters, so if the original data deals with such things, please process it appropriately.

For example, suppose you have a CSV file like this with the column name on the first line.

 1, apple, 100
 2, mandarin oranges, 80
 3, banana, 60
 4, Melon, 2000
 5, strawberry, 300

You will need such a yaml file to handle with Fixtures.

  id: 1
 item: apples
  price: 100

  id: 2
 item: Mandarin orange
  price: 80

  id: 3
 item: banana
  price: 60

  id: 4
 item: Melon
  price: 2000

  id: 5
 item: strawberry
  price: 300

The place of data1: is the fixtures key, which is not reflected in the database, but it seems that you have to give a unique value as a hash for each record.

The script looks like this. After converting from Excel to CSV, UTF-8 has a BOM, so there is an option when opening the file.

!/use/bin/env ruby

filename = ARGV[0]

# Option to erase UTF-8 BOM
file = open(filename, 'r:BOM|UTF-8')

# Get the first line of the CSV file
column = file.gets.chomp.split(",")

# The fixtures key prefix (data) followed by the counter (num)
prefix = "data"
num = 1
str = ""

 file.each do |line|
 unless line.strip == "" ## Ignore blank lines
    str += "#{prefix}#{num}:\n"
     (0 .. column.length - 1).each do |i|
      str += "  #{column[i]}: #{line.chomp.split(",")[i]}\n"
    str += "\n"
    num += 1
print str

It looks like this when running from Shell.

$ ./csv2yaml.rb fruits.csv > fruits.yml

There was no problem converting CSV data of about 500 lines, and it seems to be okay because it was imported by fixtures. I was able to convert multiple CSV files with different columns.

It may be easier to make if you use yaml library etc.

Recommended Posts

Script to make yaml from CSV to put initial data in Rails with Fixtures
Initial data input with [Rails] seed_fu!
[Rails 6] Two methods to import multiple images at once using CarrierWave / (1) Input with initial seed data / (2) Import with CSV
How to store data simultaneously in a model associated with a nested form (Rails 6.0.0)
How to make a unique combination of data in the rails intermediate table
One way to redirect_to with parameters in rails
I want to play with Firestore from Rails
How to make a follow function in Rails
How to make batch processing with Rails + Heroku configuration
Setup with initial test data inserted in Db2 / DB container
How to make an almost static page with rails
[How to insert a video in haml with Rails]
How to query Array in jsonb with Rails + postgres
[Rails] Create initial data with seed.rb [Faker] [Japanese localization]
I tried to make an application in 3 months from inexperienced
Let's write how to make API with SpringBoot + Docker from 0
Things to keep in mind when using Sidekiq with Rails
I tried to create an API to get data from a spreadsheet in Ruby (with service account)
I want to import the pull-down menu items when submitting a form in Rails into CSV and display them from the DB data.