简体   繁体   中英

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. 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)

UPDATE

The script works but everything is filled with "nill"... Here are my codes:

#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:

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 generate model Transaction street:text city:string etc...

see section 5 on the gist for appropriate columns.

Update: You should've specified delimiter for your CSV file like this:

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). 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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM