2014-07-26 2 views
0

큰 csv 파일을 Rails 프로젝트로 가져와야합니다. 내가 사용 : 루비 2.1.2p95 레일 4.1.1 MySQL의갈퀴 작업을 사용하여 외래 키를 채우는 가장 좋은 방법은 무엇입니까?

14.14 버전 내가 처음 독립 테이블에서 레코드를 작성하여, 갈퀴 작업으로이 작업을 수행하기 위해 노력하고있어, 이들의 레코드 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 개의 문장이 너무 많습니다.

관련 문제