[英]Error Transaction.new into Rails app trying to import CSV data
I try to import a CSV file into my database in a Rails app. 我尝试在Rails应用程序中将CSV文件导入到我的数据库中。 I follow this gist .
我遵循这一要旨 。
Here is my code: 这是我的代码:
# db/seeds.rb
require 'csv'
csv_text = File.read(Rails.root.join('lib', 'seeds', 'siren_db.csv'))
csv = CSV.parse(csv_text, :headers => true, :encoding => 'ISO-8859-1')
csv.each do |row|
t = Transaction.new
t.siren = row['siren']
t.nom = row['nom']
t.adresse = row['adresse']
t.complement_adresse = row['complement_adresse']
t.pays = row['pays']
t.region = row['region']
t.departement = row['departement']
t.activite = row['activite']
t.date = row['date']
t.nb_salaries = row['nb_salaries']
t.nom = row['nom']
t.prenom = row['prenom']
t.civilite = row['civilite']
t.adr_mail = row['adr_mail']
t.libele_acti = row['libele_acti']
t.categorie = row['categorie']
t.tel= row['tel']
t.save
puts "#{t.siren}, #{t.nom} saved"
end
puts "There are now #{Transaction.count} rows in the transactions table"
Unfortunately, I have an error but don't know why? 不幸的是,我有一个错误,但不知道为什么? (I have the exact same code as the gist) :
(我有与要旨完全相同的代码):
rake aborted!
耙子流产了! NameError: uninitialized constant Transaction /Users/nicolasleroux/Public/sites/sirenforest/db/seeds.rb:6:in block in ' /Users/nicolasleroux/Public/sites/sirenforest/db/seeds.rb:5:in' Tasks: TOP => db:seed (See full trace by running task with --trace)
NameError:未初始化的常量事务/Users/nicolasleroux/Public/sites/sirenforest/db/seeds.rb:6:在'/Users/nicolasleroux/Public/sites/sirenforest/db/seeds.rb:5:in'中的块:TOP => db:seed(通过使用--trace运行任务来查看完整跟踪)
The script works but everything is filled with "nill"... Here are my codes: 该脚本有效,但是所有内容都充满了“ nill” ...这是我的代码:
#db/migrate/create_transaction
class CreateTransactions < ActiveRecord::Migration[5.0]
def change
create_table :transactions do |t|
t.integer :siren
t.string :nom_ent
t.string :adresse
t.string :complement_adresse
t.string :pays
t.string :region
t.integer :departement
t.string :activite
t.integer :date
t.string :nb_salaries
t.string :nom
t.string :prenom
t.string :civilite
t.string :adr_mail
t.string :libele_acti
t.string :categorie
t.integer :tel
t.timestamps
end
end
end
#model transaction
class Transaction < ApplicationRecord
end
The beginning of the csv file: csv文件的开头:
SIREN;NOM;ADRESSE;COMPLEMENT_ADRESSE;CP_VILLE;PAYS;REGION;DEPARTEMENT;ACTIVITE;DATE;NB_SALARIES;NOM;PRENOM;CIVILITE;ADR_MAIL;LIBELE_ACTI;CATEGORIE;TEL
38713707;SYND COPR DU 6 AU 8 RUE DE CHARONNE 75;6 RUE DE CHARONNE;;75011 PARIS;FRANCE;Île-de-France;75;Activités combinées de soutien lié aux bâtiments;2008;1 ou 2 salariés;;;;;Syndicat de copropriété ;PME;
38713707;SYND COPR DU 6 AU 8 RUE DE CHARONNE 75;6 RUE DE CHARONNE;;75011 PARIS;FRANCE;Île-de-France;75;Activités combinées de soutien lié aux bâtiments;2008;1 ou 2 salariés;;;;;Syndicat de copropriété ;PME;
38724340;SYND COPR DU 18 BD ARAGO 75013 PARIS;18 BOULEVARD ARAGO;;75013 PARIS;FRANCE;Île-de-France;75;Activités combinées de soutien lié aux bâtiments;2008;1 ou 2 salariés;;;;;Syndicat de copropriété ;PME;
look at the 1. Setup section it says like this: 看一下“设置”部分,内容如下:
Make sure you've created a resource with the appropriate columns to match your seed data.
确保已创建具有适当列的资源以匹配您的种子数据。 The names don't have to match up.
名称不必匹配。
You must generate Transaction model in your rails application, like this: 您必须在rails应用程序中生成事务模型,如下所示:
$ rails generate model Transaction street:text city:string etc...
see section 5 on the gist for appropriate columns. 请参阅要点上的第5节以获取适当的列。
Update: You should've specified delimiter for your CSV file like this: 更新:您应该为CSV文件指定分隔符,如下所示:
csv = CSV.parse(csv_text, :headers => true, :encoding => 'ISO-8859-1', :col_sep => ';' )
also hash key should have been uppercase as in your csv file and you have same column names, should be unique(t.nom). 哈希键也应该像在csv文件中一样大写,并且您具有相同的列名,并且应该是唯一的(t.nom)。 Full code:
完整代码:
csv = CSV.parse(csv_text, :headers => true, :encoding => 'ISO-8859-1', :col_sep => ';' )
csv.each do |row|
t = Transaction.new
t.siren = row['SIREN']
t.nom = row['NOM'] # => 2 same columns
t.adresse = row['ADRESSE']
t.complement_adresse = row['COMPLEMENT_ADRESSE']
t.pays = row['PAYS']
t.region = row['REGION']
t.departement = row['DEPARTEMENT']
t.activite = row['ACTIVITE']
t.date = row['DATE']
t.nb_salaries = row['NB_SALARIES']
t.nom = row['NOM'] # => 2 same columns
t.prenom = row['PRENOM']
t.civilite = row['CIVILITE']
t.adr_mail = row['ADR_MAIL']
t.libele_acti = row['LIBELE_ACTI']
t.categorie = row['CATEGORIE']
t.tel= row['TEL']
t.save
puts "#{t.siren}, #{t.nom} saved"
end
puts "There are now #{Transaction.count} rows in the transactions table"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.