Import a CSV file into several models at a time

I have 3 models, a church in which there are many places in which there are many pastors.

require 'csv' csvfile = File.read("testimport.csv") csv = CSV.parse(csvfile, :headers => false) csv.each do |row| c = Church.new c.name = row[0] c.url = row[10] c.locations.build(:address => row[3], :zipcode => row[5], :phone => row[6], :email => row[2], :city => row[4]) c.save end 

As you can see in my short block of code, I am creating a church and its first place. How do I add a pastor to this?

For example, will this work?

 require 'csv' csvfile = File.read("testimport.csv") csv = CSV.parse(csvfile, :headers => false) csv.each do |row| c = Church.new c.name = row[0] c.url = row[10] location = c.locations.build(:address => row[3], :zipcode => row[5], :phone => row[6], :email => row[2], :city => row[4]) location.pastors.build(:name => row[10]) location.save c.save end 

Is there any other way that I should do this? Trying to move thousands of records from one rails application to another.

+6
source share
2 answers

I was able to get this to work, that's what I used. Let me know if there is a more reasonable way to do this. NOTE. If you are trying to do this, put the csv file in the root directory of your rails directory and run this script line by line in the console. At least this is how I got started.

 require 'csv' csvfile = File.read("testimport.csv") csv = CSV.parse(csvfile, :headers => false) csv.each do |row| c = Church.new c.name = row[0] c.url = row[10] c.locations.build(:title => "Sanctuary", :address => row[3], :zipcode => row[5], :phone => row[6], :email => row[2], :city => row[4]) c.save loc = c.locations.first loc.pastors.build(:firstname => row[1]) loc.save end 
0
source

I approach this a bit, I found that the two-step process is easier to use and use.

The first step is to download the data.

I use two staging tables.
Sort of:

 staging_header id Integer Unique Primary Key run_number Integer Unique run_name String staging_data: id Integer Unique Primary Key staging_header_id Integer element1 String element2 String element3 String uploaded? Boolean # Placed on the individual records allows restarts. ... 

So, I load testimport.csv directly into these load tables that support multiple runs if you make a unique run_number number (sequence, etc.)

Now you have data in sql and are available in rails.

Now write the code to really populate the application tables from this download area.

It will also help you deal with the problem of speed. Rails will only insert a few records per second, so you want to be able to reload, pause, etc.

This will also help with validation. Initially, you just want to load data regardless of any restrictions (not null, unique, etc.).

After loading into the queue, you can be more selective and apply validations as you wish.

+1
source

All Articles