TomEus
TomEus

Reputation: 254

undefined local variable or method `column' for main:Object when running csv import via rake

I am trying to learn Rails and one of the first exercises I wanted to lear was to automate csv import into database. So I created the model by using

rails g scaffold Domain  dns:string current_bid:integer join_by_pt:datetime bidders:integer seller:string tld:string length:integer words:string categories:string hyphnens:string numbers:string auction_type:string

Next I created the rake file:

require 'csv'

desc "Import domains from csv file"
  task :import => [:environment] do

  file = "db/dl.txt"

  CSV.foreach(file, :headers => true) do |row|
  entry = Domain.find_or_create_by(:dns => column[1])
  entry.bid = column[2]
  entry.join_date = DateTime.parse(column[3])
  entry.bidders = column[4]
  entry.seller = column[5]
  entry.tld = column[6]
  entry.length = column[7]
  entry.words = column[8]
  entry.categories = column[9]
  entry.hyphens = column[10]
  entry.numbers = column[11]
  entry.auction_type = column[12]
  entry.save 
 end

end

But when I try to run it, I get the error:

> rake aborted! undefined local variable or method `column' for
> main:Object /Users/user/drop/lib/tasks/import.rake:9:in `block (2
> levels) in <top (required)>'
> /Users/uer/drop/lib/tasks/import.rake:8:in `block in <top (required)>'

Any pointers as to what I am doing wrong and where my line of thinking went to the wrong direction please?

Thank you

Upvotes: 1

Views: 1140

Answers (1)

Rajarshi Das
Rajarshi Das

Reputation: 12320

try this make all of your column to row

desc "Import domains from csv file"
 task :import => [:environment] do

 file = "db/dl.txt"

 CSV.foreach(file, :headers => true) do |row|
  entry = Domain.new
  entry.dns =  row[1] unless row[1].nil?  
  entry.bid = row[2] unless row[2].nil?
  entry.join_date = DateTime.parse(row[3]) unless row[3].nil?
  entry.bidders = row[4] unless row[4].nil?
  entry.seller = row[5] unless row[5].nil?
  entry.tld = row[6] unless row[6].nil?
  entry.length = row[7] unless row[7].nil?
  entry.words = row[8] unless row[8].nil?
  entry.categories = row[9] unless row[9].nil?
  entry.hyphens = row[10] unless row[10].nil?
  entry.numbers = row[11] unless row[11].nil?
  entry.auction_type = row[12] unless row[12].nil?
  entry.save 
 end
end

Upvotes: 1

Related Questions