2014-07-26 94 views
0

我需要將一個大的csv文件導入到Rails項目中。我使用的是: 紅寶石2.1.2p95 的Rails 4.1.1 的MySQL版本14.14使用rake任務填充外鍵的最佳方式是什麼?

我試圖做到這一點作爲一個rake任務,由獨立的表中首先創建的記錄,得到記錄id的那些記錄,並將這些外鍵用於填充相關表中的記錄。

我想創建只是獨立的記錄,並打印出這些外國id,首先在一個小的測試文件上運行。

不過,我得到這個檔案結尾我不明白:

rake aborted! 
SyntaxError: /Users/rickcasey/Projects/Programming/WikiFrac/wfrails/lib/tasks/import_partial.rake:94: 

語法錯誤,意想不到的keyword_end,期待

此輸入結束是我的耙子腳本看起來像:

#lib/tasks/import_partial.rake 
require 'csv' 

# Independent tables: 
# Companies 
# Counties 
# Fields 
# Formations 
# Gastypes 
# Wells 
# 
# Dependendecies and foreign key field used to find correct record id: 
# Facilities.company_id -> Companies.company_name 
# Facilities.field_id -> Fields.field_name 
# Facilities.county_id -> Counties.county_name 
# Wells.gastype_id  -> GasTypes.gas_type 

task :import_partial => :environment do  
    csv.foreach('public/partial.csv', :headers => true) do |row| 

      # create records in independent tables 

      # create the Company object 
      this_company_name = row.to_hash.slice(*%w[county_name]) 
      if !(Company.exists?(company_name: this_company_name)) 
       Companies.create(row.to_hash.slice(*%w[company_name operator_num])) 
      end 
      thecompany = Company.find(this_company_name) 
      company_id = thecompany.id 

      # create the County object 
      this_county_name = row.to_hash.slice(*%w[county]) 
      if !(County.exists?(county_name: this_county_name)) 
       Counties.create(county_name: this_county_name) 
      end 
      thecounty = County.find(this_county_name) 
      county_id = thecounty.id 

      # create the GasType object 
      this_gastype_name = row.to_hash.slice(*%w[gas_type]) 
      if !(GasType.exists?(gastype_name: this_gastype_name)) 
       GasType.create(gastype_name: this_gastype_name) 
      end 
      thegastype = GasType.find(this_gastype_name) 
      gastype_id = thegastype.id 


      # create the Field object 
      this_field_name = row.to_hash.slice(*%w[field]) 
      if !(Field.exists?(field_name: this_field_name)) 
       Field.create(field_name: this_field_name, field_code: field_code) 
      end 
      thefield = Field.find(this_field_name) 
      field_id = thefield.id 

      # create the Formations object 
      this_formation_name = row.to_hash.slice(*%w[formation]) 
      if !(Formation.exists?(formation_name: this_formation_name)) 
       Counties.create(formation: this_formation_name, formation_code: formation_code) 
      end 
      theformation = Formation.find(this_formation_name) 
      formation_id = theformation.id 

      # debugging: 
      puts "company_id:", company_id 
      puts "county_id:", county_id 
      puts "gastype_id:", gastype_id 
      puts "field_id:", field_id 

      # create records in dependent tables: 
      # Use the record id's from above independent table create records containing foreign keys: 

      #Facilities.create(row.to_hash.slice(*%w[dir_e_w dir_n_s dist_e_w dist_n_s facility_name facility_num ground_elev lat long meridian qtrqtr range sec twp utm_x utm_y]) 

      #Wells.create(row.to_hash.slice(*%w[api_county_code api_seq_num first_prod_date form_status_date formation_status sidetrack_num spud_date status_date td_date test_date wbmeasdepth wbtvd well_bore_status well_name]) 


     end 
    end 
end 

任何建議非常感謝...

回答

3

您的文件末尾有太多的end聲明。

相關問題