I have 3 models, Church, which has many Locations, which has 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 location. How would I also add a pastor to that?
For instance 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 another way I should be going about this? Trying to move thousands of records from one rails app to another.
I was able to get this to work, the following is what I used. Let me know if there is a smarter way to do this. NOTE: if you are trying to do this, place the csv file in the root of your rails directory and execute this script line by line in the console. At least thats how I got it working.