简体   繁体   中英

importing data from csv into rails application

I am trying to import data from CSV into the database using Classes so that I can easily write Test Case for the csv import rake task I created

However, my solution does not work .

And I also feel:

  1. It doesn't make sense
  2. Aside feeling its not a good solution that connotes Ruby mastery, it doesn't work.

Here is what I came up with in my engines/csv_importer/lib/tasks/csv_import.rake

require 'open-uri'
require 'csv'

namespace :csv_import do
  desc 'Import users from csv'

  task users: :environment do
    WebImport.new(url: 'http://blablabla.com/details/people.csv').call.answers
  end
end

class WebImport
  def initialize(url)
    @csv_string = url
  end

  def call
    CSV.parse(@csv_string, headers: true, header_converters: :symbol) do |row|
      next unless row[:name].present? && row[:email_address].present?
    end
    CsvImporter::User.create row.to_h
  end

  def self.answers
    user = []
    counter = 0
    duplicate_counter = 0
    user.persisted? ? counter + 1 : duplicate_counter + 1
    p "Email duplicate record: #{user.email_address} - #{user.errors.full_messages.join(',')}" if user.errors.any?
    p "Imported #{counter} users, #{duplicate_counter} duplicate rows ain't added in total"
  end

end

Error when I run rake csv_import:users

$ rake csv_import:users
rake aborted!
NoMethodError: private method `gets' called for {:url=>"http://blablabla.com/details/people.csv"}:Hash

How do I make this work and write TEST for this at the long run?

You are getting this error because you are passing a hash to CSV.parse while that method accepts a string .

To fix that you need to change argument from a hash to a string: WebImport.new('http://blablabla.com/details/people.csv') and read a remote CSV file before passing it to CSV.parse , for example: CSV.parse(open(url)) .

You can try to use

rake db:seed

to import the data to your database using seed file as

require 'csv'

puts "Importing data..."
CSV.foreach(Rails.root.join("file_name.csv"), headers: true) do |row|
  Model_name.create! do |model_name|
    model_name.name = row[0]
    model_name.email_address = row[1]
  end
end

csv file should be in your project root folder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM