使用rake任务填充外键的最佳方法是什么?

时间:2022-09-20 18:49:32

I need to import a big csv file into a Rails project. I'm using: ruby 2.1.2p95 Rails 4.1.1 mysql Ver 14.14

我需要将一个大的csv文件导入到Rails项目中。我正在使用:ruby 2.1.2p95 Rails 4.1.1 mysql Ver 14.14

I'm trying to do this as a rake task, by first creating records in the independent tables, get the record id's for those records, and use these for the foreign keys to populate records in the dependent tables.

我正在尝试将此作为rake任务,首先在独立表中创建记录,获取这些记录的记录ID,并使用这些记录在外键中填充依赖表中的记录。

I'm trying to create just the independent records, and print out these foreign id's, operating on a small test file first.

我正在尝试创建独立的记录,并打印出这些外来id,首先在一个小的测试文件上运行。

However, I'm getting this end-of-file I don't understand:

但是,我收到这个文件结尾我不明白:

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

syntax error, unexpected keyword_end, expecting end-of-input

语法错误,意外的keyword_end,期望输入结束

This is what my rake script looks like:

这是我的rake脚本的样子:

#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

Any suggestions much appreciated...

任何建议非常感谢...

1 个解决方案

#1


3  

You have 1 too many end statements at the end of your file.

在文件末尾有1个太多的结束语句。

#1


3  

You have 1 too many end statements at the end of your file.

在文件末尾有1个太多的结束语句。