I get an error invalid byte sequence in UTF-8
when trying to import a CSV file into my Rails application. Everything worked fine until I added a method gsub
to compare one of the CSV columns with a field in my database.
When I import a CSV file, I want to check if the address for each line is included in an array of different addresses for a particular client. I have a client model with a property alt_addresses
that contains several different possible formats for the client address.
Then I have a citation model (if you are familiar with local SEO, you will know this term). The citation model does not have an address field, but has a field nap_correct?
(NAP means "name", "address", "phone number"). If the name, address and phone number for the CSV string are equivalent to what I have in the database for this client, the field nap_correct?
for this quote will be set to "correct".
Here is what the method looks like import
in my citation model:
def self.import(file, client_id)
@client = Client.find(client_id)
CSV.foreach(file.path, headers: true) do |row|
@row = row.to_hash
@citation = Citation.new
if @row["Address"]
if @client.alt_addresses.include?(@row["Address"].to_s.downcase.gsub(/\W+/, '')) && self.phone == @row["Phone Number"].gsub(/[^0-9]/, '')
@citation.nap_correct = true
end
end
@citation.name = @row["Domain"]
@citation.listing_url = @row["Citation Link"]
@citation.save
end
end
And here the property looks alt_addresses
in my client model:
def alt_addresses
address = self.address.downcase.gsub(/\W+/, '')
address_with_zip = (self.address + self.zip_code).downcase.gsub(/\W+/, '')
return [address, address_with_zip]
end
gsub
, CSV, , . . gsub
, .
Ruby 2.1.3. , , , , Ruby.