Having trouble seeding CSV file into Rails App Having trouble seeding CSV file into Rails App sqlite sqlite

Having trouble seeding CSV file into Rails App


Best success I've seen with this is by doing a rake task.

require 'csv'namespace :csv do  desc "Import CSV Data"  task :import_stuff => :environment do    csv_file_path = 'db/data.csv'    CSV.foreach(csv_file_path) do |row|      Model.create!({        :column1 => row[0],        :column2 => row[1],        :column3 => row[2],              })      puts "Row added!"    end  endend

Put this in your lib/tasks folder with a .rake extension and to run it type: "rake csv:import_stuff"

Also, you might be reaching some limitations with SQL lite... I would suggest checking out MongoDB. It seems like it would be fitting for your current situation. Good luck!


Looks like I figured it out, sorry about that. I would appreciate some comments on this code, if it needs to be improved.

Anyway, I think I figured it out using fasterCSV (or just CSV). This seems to have worked:

require 'csv'Model.delete_allCSV.foreach("#{Rails.root}/lib/data/model.csv") do |row|     Model.create!(:attr_1 => row[0], :attr_2 => row[1], :attr_3 => row[2], etc)end