views:

34

answers:

2

This may well be a duplicate, but I couldn't find anyone asking quite this question.

My understanding* is that if I want to migrate data from an outside source to my Rails app's database, I should do so using a migration. It seems from my preliminary research that what I could do is use a tool like FasterCSV to parse a CSV file (for example) right in the migration (.rb) file itself.

Is this the correct approach? And if so, where should I actually put that CSV file -- it seems that if migrations are, after all, meant to be reversible/repeatable, that CSV data ought to be kept in a stable location.

*Let me know if I am completely mistaken about how to even go about this as I am still new to RoR.

+1  A: 

You can write this to a rake job without FasterCSV, though I use both.

Write rows to 'csvout' file.

outfile = File.open('csvout', 'wb')
CSV::Writer.generate(outfile) do |csv|
  csv << ['c1', nil, '', '"', "\r\n", 'c2']
  ...
end

outfile.close

This file will output where the rake file is written. In your case, you can put it in a seperate folder for CSV's. I would personally keep it out of the rest of the app structure.

Trip
+1  A: 

You may want to look into seed_fu to manage it. It has the benefit of being able to easily update the data already in database. You can convert the CSV into a seed file, which is just a Ruby code (example code is provided there).

htanata