-2

I need to seed a database with a lot of records from some Excel files, but I will only use those CSV files once because after seeding the database the other records will be input from forms.

My boss told me to put the CSV files in a directory in the project and then get the records from those files to the database.

I'm using MongoDB on Rails.

I don't want the full code, just some guidance with gems, logic, etc.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Miguel Rebola
  • 120
  • 1
  • 2
  • 12
  • 1
    Most DBMs have ways of importing directly from CSV files. Why not take advantage of their built-in, pre-tested, executables rather than write your own? – the Tin Man May 08 '15 at 00:06

2 Answers2

1

If it's for a one-off import, why not use something like this nice gist or MongoDB's own tool, mongoimport (which can accept CSVs)

Mario
  • 1,349
  • 11
  • 16
0

You need the CSV library. From the docs:

arr_of_arrs = CSV.read("path/to/file.csv")

This will give you a 2D array which you can process as you like. CSV similar to IO.read, but with a few extras such as header parsing.

Brian Davis
  • 357
  • 3
  • 7