Skip to content

Instantly share code, notes, and snippets.

@Brikky
Last active August 26, 2017 20:21
Show Gist options
  • Save Brikky/ab15b404e3f9e2f0e2c981808e5562e2 to your computer and use it in GitHub Desktop.
Save Brikky/ab15b404e3f9e2f0e2c981808e5562e2 to your computer and use it in GitHub Desktop.

1.Create a folder inside of lib called seeds
2. Put your CSV file example.csv into the lib/seeds folder.
3.

#model needs to already exist in the DB, even if no instances
    require 'csv'

    csv_text = File.read(Rails.root.join('lib', 'seeds', 'file.csv'))
    csv = CSV.parse(csv_text, :headers => true, :encoding => 'UTF-8')
    csv.each do |row|
      m = Model.new
      m.field = row.field
      m.save
    end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment