Reputation: 46
I'm importing a thousand record csv to a model that has gmap4rails geocoding. The import keeps failing after a dozen or so records, but always on a different record. The data doesn't seem bad, as it has imported a record on one try, and then failed on that same record on another. Here's the model code:
require 'csv'
validates :name, :uniqueness => true
validates :address, :presence => true
acts_as_gmappable
def gmaps4rails_address
"#{self.address} New Orleans, LA #{self.zip}"
rescue ActiveRecord::RecordInvalid
print "err"
end
def self.import_restaurants()
CSV.foreach('lib/uploads/NOLA_Restaurants_Basic.csv', :headers => true) do |row|
record = Restaurant.new(
:name => row[0],
:address => row[1],
:zip => row[2],
)
if record.address.valid?
record.save!
end
end
end
I'm running Restaurant.import_restaurants() from the console.
A few sample lines of the CSV
name address zip
ORLEANS CLUB 5005 ST CHARLES AVE 70115
LOUISIANA CLUB 707 UNION ST 70130
HOTEL MONTELEONE 214 ROYAL ST 70130
N O LAWN TENNIS CLUB 5353 LAUREL ST 70115
DOMILISES BAR 5240 ANNUNCIATION ST 70115
Upvotes: 1
Views: 404