Having trouble seeding CSV file into Rails App

Viewed 3457

I'm trying to seed about 2K records into a model and I've tried using all the different methods out there so far (faster csv, fast_seeder, and the railscasts ep). I feel like this should be pretty simple. I've got all the data in a CSV file and I've got the model set up already, ready to be seeded.

The only thing that's worked so far is what's shown in the RailsCasts episode. I plugged in this code for my attributes, and it seeded only the first line (out of 2K), and that's it:

Country.delete_all
open("#{Rails.root}/path_to_my_file") do |models|
  models.read.each_line do |model|
     column_1, column_2, column_3, column_4 = model.chomp.split(",")
     Model.create!(:attr_1 => column_1, :attr_2 => column_2, ...etc)
  end
end

Then I tried using FasterCSV based on some of the other questions, but I received a bunch of errors saying that fastercsv was already included in the latest release of ruby, I couldn't seem to figure it out (this could be my fault, but I haven't been able to find a good SO question that lays it out nicely).

Finally, Fast_Seeder seemed to have some potential, and it recognized all my entries, but didn't save any of them into the model because I received this error:

SQLite3::SQLException: too many terms in compound SELECT:(all my columns)

Anyway, again I feel like this should be simple enough. I just have a CSV with 2K entries, and a blank model that I need to seed. Best way to do this would be much appreciated, thanks!

2 Answers
Related