0

I have a CSV with a list of user email addresses. I need to add these addresses as new records to my heroku user table without affecting the user rows that are already in there. The CSV is not currently in the same table format as the heroku table, it is simply one column of data.

I have tried a few methods, however they seem to start by removing the data that is currently in the table before importing. What is the best way to achieve this?

3
  • Email list is in same order as records there on heroku? Means first email in csv should match to 1st record in user table and so on ? Commented Sep 5, 2014 at 10:09
  • The email addresses are not for the current records. They are to be added as new records in the table. Each email address in my CSV should lead to one new record in my user table. They are in no particular order. Commented Sep 5, 2014 at 10:16
  • So simply create a seed file for this. How many column your csv file have ? Commented Sep 5, 2014 at 10:22

1 Answer 1

1

considering you have csv with head "email"

Your seed file will look like :

require 'csv'

data = CSV.read(Rails.root.to_s + '/public/csvfile', headers: true)
data['email'].each do |email|
  @user = User.new(:email => email)
  User.save(:validate => false)
end
Sign up to request clarification or add additional context in comments.

2 Comments

After a few tweaks, this has worked perfectly. Thanks Nitin :)
It'll be horrifyingly slow for large user counts, though. If you want to do it efficiently, COPY the csv into a new table, then do an UPDATE ... FROM to do a joined update adding the new field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.