How can I import a CSV file via a rake task?

后端 未结 5 607
梦如初夏
梦如初夏 2020-12-29 05:28

I know this question has been asked a lot on this forum but I\'m under a strict deadline and I need some help, so any advice is much appreciated. I\'m new to Ruby on Rails

相关标签:
5条回答
  • 2020-12-29 05:53

    I worked on this for hours and hours one day. I finally got it to work by doing the following:

    1. Added a header in the first row to my csv file that reflected the attr_accessible in my model. In my case my model was attr_accessible :intro, :name and in my csv file the first line read name, intro.
    2. Created a custom rake file. I named mine import.rake and placed it in the lib/tasks folder. Place this code in that file:
    #lib/tasks/import.rake
    require 'csv'
    desc "Imports a CSV file into an ActiveRecord table"
    task :import, [:filename] => :environment do    
        CSV.foreach('myfile.csv', :headers => true) do |row|
          MyModel.create!(row.to_hash)
        end
    end
    

    Then type bundle exec rake import into the command line.

    To get this to work I had quite SQLite Database Browser. I hope that helps someone!

    0 讨论(0)
  • 2020-12-29 05:53

    Here is an example CSV that I imported using rake db:seed. I wrote this into the seeds.rb file and put the CSV file into /public/seed_data/zip_code.csv. It's pretty self explanatory (i.e., the csv has three columns: code, long. and lat.

    The code parses each line, extracts the pertinent data and assigns it to a local variable then writes it to a record. Hope it helps.

    File.open("#{Rails.root}/public/seed_data/zip_code.csv") do |zip_codes|
      zip_codes.read.each_line do |zip_code|
        code, longitude, latitude = zip_code.chomp.split(",")
        #  to remove the quotes from the csv text:
        code.gsub!(/\A"|"\Z/, '')
        # to create each record in the database
        ZipCodeGeo.create!(:zip_code => code, :longitude => longitude, :latitude =>      latitude)             
      end
    end
    
    0 讨论(0)
  • 2020-12-29 05:58

    You can read your csv file using CSV module of rails and can create records. Here is detailed help: Populate db using csv

    0 讨论(0)
  • 2020-12-29 06:05

    under your project folder in lib/task create a rake file say "import_incidents_csv.rake"

    follow this Ruby on Rails - Import Data from a CSV file

    in rake file have following code

    require 'csv'
    namespace :import_incidents_csv do
      task :create_incidents => :environment do
        "code from the link"  
      end
    end 
    

    You can call this task as "rake import_incidents_csv:create_incidents"

    0 讨论(0)
  • 2020-12-29 06:16

    I've used this one in the past. It takes any type of model.

    rake csv_model_import[bunnies.csv,Bunny]

    Works like a charm.

    desc "Imports a CSV file into an ActiveRecord table"
    task :csv_model_import, :filename, :model, :needs => :environment do |task,args|
      lines = File.new(args[:filename]).readlines
      header = lines.shift.strip
      keys = header.split(',')
      lines.each do |line|
        params = {}
        values = line.strip.split(',')
        keys.each_with_index do |key,i|
          params[key] = values[i]
        end
        Module.const_get(args[:model]).create(params)
      end
    end

    0 讨论(0)
提交回复
热议问题