好的,让我问另一种方式。这是我要以csv格式导入的一个临时表的示例。
id:
incident_datetime: 2012-09-24 08:07:00.000000+0000
location: Hamburg
report_nr: 33-11-00201
responsible_party: John
area_resident: No
street: 6108 FoxHunt
city: Lexington
state: Ky
home_phone: 111-111-1111
cell_phone: 222-222-2222
insurance_carrier_name: Nationwide
insurance_carrier_street: Waller
insurance_carrier_city: Lexington
insurance_carrier_state: KY
insurance_carrier_phone: 666-666-6666
insurance_carrier_contact: Jason
policy_nr: 2sdf2sdf
vin_nr: ZTZHK3fhr5546107407
license_nr: 520-VDH
vehicle_make: Toyota
vehicle_model: Tacoma
vehicle_year: 2004
created_at:
updated_at:
status: Recieved
user_id: 16
resp_zip: 40509
insur_zip: 40509
这是rakefile的代码。
require 'csv'
namespace :import_incidents_csv do
task :create_incidents => :environment do
puts "Import Incidents"
csv_text = File.read('c:/rails/thumb/costrecovery_csv/lib/csv_import/incidents.csv')
csv = CSV.parse(csv_text, :headers => true)
@incident_id_array = []
csv.each do |row|
row = row.to_hash.with_indifferent_access
Incident.create!(row.to_hash.symbolize_keys)
@incident_id_array << Incident.last.id
end
end
end
当顶部的id被转储到数据库中时,将自动为其分配一个值。然后我将该id值保存在一个数组中并为其创建时间表。这就是时间表的样子。
id:
name:
date:
incident_id: x
created_at:
updated_at:
需要创建时间表,因为一堆模型“属于它”。这是创建它的rakefile:
require 'csv'
namespace :import_timesheets_csv do
task :create_timesheets => :environment do
puts "Import Timesheets"
csv_text = File.read('c:/rails/thumb/costrecovery_csv/lib/csv_import/timesheets.csv')
csv = CSV.parse(csv_text, :headers => true)
@timesheet_id_array = []
csv.each_with_index do |row,index|
row = row.to_hash.with_indifferent_access
Timesheet.create!(row.to_hash.symbolize_keys)
@timesheet_id_array << Timesheet.last.id
timesheet = Timesheet.last
timesheet.incident_id = @incident_id_array[index]
timesheet.save
end
end
end
所以在这一点上,我创建了一个新事件,它与我刚刚创建的新时间表相关联。现在我要导入一个创建safety_officers的csv文件。这是文件的样子。
id:
name: Safety Officer 1
first_hour: 1
additional_hours: 2
total_hours: 3
hazmat_hours: 0.5
rate1:
rate2:
timesheet_id:
created_at:
updated_at:
以下是导入safety_officer.csv文件的代码。
require 'csv'
namespace :import_safety_officers_csv do
task :create_safety_officers => :environment do
puts "Import Safety Officers"
csv_text = File.read('c:/rails/thumb/costrecovery_csv/lib/csv_import/safety_officers.csv')
csv = CSV.parse(csv_text, :headers => true)
csv.each_with_index do |row,index|
row = row.to_hash.with_indifferent_access
SafetyOfficer.create!(row.to_hash.symbolize_keys)
safety_officer = SafetyOfficer.last
safety_officer.timesheet_id = @timesheet_id_array[index]
safety_officer.save
end
end
end
以上是导入1个事件报告,导入/创建1个时间表以及导入1个安全员的示例。我正在试图找出我的代码需要如何修改,以便安全人员知道他们属于哪个事件报告。如何为它们分配与其事件相对应的正确timesheet_id。我还有其他几个模型我也必须这样做,但我只需要弄清楚其中一个,然后我可以复制它。所有这些都保存为不同的rake文件,并由一个主rake任务执行,如下所示。
require 'csv'
namespace :combine_csv do
task :combine => :environment do
Rake::Task["import_incidents_csv:create_incidents"].invoke
Rake::Task["import_timesheets_csv:create_timesheets"].invoke
Rake::Task["import_safety_officers_csv:create_safety_officers"].invoke
end
end