dimanche 15 janvier 2017

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)

Aucun commentaire:

Enregistrer un commentaire