如何通过rake任务导入CSV文件? [英] How can I import a CSV file via a rake task?

查看:128
本文介绍了如何通过rake任务导入CSV文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经在这个论坛上被问了很多,但我在一个严格的截止日期,我需要一些帮助,所以任何建议是非常赞赏。我是Ruby on Rails的新手,所以请在回复时记住。我想创建一个rake任务,当运行时,更新mysqlite db中的多个表。这是一个在我的数据库中创建新事件的迁移文件。如何创建一个rake任务,通过csv文件输入所有这些信息。有人可以给我一些帮助,从开始写完耙文件。显然,你不需要为每个字符串编写每个任务,只是给我一些例子。除了实际的rake文件,我需要添加代码到我的应用程序的任何其他部分(我知道这是一个非常一般的问题,但如果我需要添加代码,我将欣赏对其中的一般描述)。我很抱歉的新手问题,但我觉得有点指导会一路走,所以谢谢任何人回应。如果任何人需要我的任何更多的信息,请只问问谢谢。这个论坛过去帮助了我很多,但这是我第一次发布。

  class CreateIncidents< ActiveRecord :: Migration 
def self.up
create_table:accidents do | t |
t.datetime:incident_datetime
t.string:location
t.string:report_nr
t.string:responsible_party
t.string:area_resident
t .string:street
t.string:city
t.string:state
t.string:home_phone
t.string:cell_phone
t.string:insurance_carrier_name
t.string:insurance_carrier_street
t.string:insurance_carrier_city
t.string:insurance_carrier_state
t.string:insurance_carrier_phone
t.string:insurance_carrier_contact
t。 string:policy_nr
t.string:vin_nr
t.string:license_nr
t.string:vehicle_make
t.string:vehicle_model
t.string:vehicle_year


t.timestamps
end

p>

def self.down
drop_table:accidents
end
end

解决方案

在lib / task下的项目文件夹下创建一个rake文件说import_incidents_csv.rake



href =http://stackoverflow.com/questions/4410794/ruby-on-rails-import-data-from-a-csv-file> Ruby on Rails - 从CSV文件导入数据 rake文件中的p>

有以下代码

  require'csv'
namespace:import_incidents_csv do
task:create_incidents => :环境do
代码从链接
end
end

您可以将此任务称为rake import_incidents_csv:create_incidents


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 so please keep that in mind when responding. I want to create a rake task that, when run, updates multiple tables in mysqlite db. This is a migration file that creates a new incident in my db. How do I create a rake task that will input all this info via a csv file. Can someone PLEASE give me some help in writing the rake file from start to finish. Obviously you don't need to write every task for every string, just give me a few examples. And besides the actual rake file, do I need to add code to any other part of my app (I know thats a very general question, but if I do need to add code, I would appreciate a general description of where). I'm sorry for the newbie question but I feel a little bit of guidance will go along way so thank you to anyone that responds. If anyone needs any more information from me please just ask thank you. This forum has helped me a lot in the past, but this is my first time posting.

class CreateIncidents < ActiveRecord::Migration
def self.up
create_table :incidents do |t|
  t.datetime :incident_datetime
  t.string :location
  t.string :report_nr
  t.string :responsible_party
  t.string :area_resident
  t.string :street
  t.string :city
  t.string :state
  t.string :home_phone
  t.string :cell_phone
  t.string :insurance_carrier_name
  t.string :insurance_carrier_street
  t.string :insurance_carrier_city
  t.string :insurance_carrier_state
  t.string :insurance_carrier_phone
  t.string :insurance_carrier_contact
  t.string :policy_nr
  t.string :vin_nr
  t.string :license_nr
  t.string :vehicle_make
  t.string :vehicle_model
  t.string :vehicle_year


  t.timestamps
end

end

def self.down drop_table :incidents end end

解决方案

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"

这篇关于如何通过rake任务导入CSV文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆