I am trying to import a CSV file into an existing table in my SQLite3 database on a rails project.
I have tried the correct answer on this solution but I get the error:
uninitialised constant Object::Points
Here is my data.csv file which is in my main directory:
Item,2003,2004,2005,2006,2007
AA2,43,34,23,52,24
TT2,48,39,29,23,29
Here is my model file which is saved in app\models\points.rb:
class Points < ActiveRecord::Base
attr_accessible :Item, :2003, :2004, :2005, :2006, :2007
end
Here is my migration file which is saved in db\migrate\20130709123346_create_points.rb:
class CreatePoints < ActiveRecord::Migration
def change
create_table :points do |t|
t.varchar(255) :Item
t.integer :"2003"
t.integer :"2004"
t.integer :"2005"
t.integer :"2006"
t.integer :"2007"
t.timestamps
end
end
end
Below is what I have tried based on the correct solution linked above:
Created new file called import.rake and saved it in lib\tasks\import.rake:
require 'csv' csv_text = File.read('data.csv') csv = CSV.parse(csv_text, :headers => true) csv.each do |row| Points.create!(row.to_hash) end
Then I ran
bundle exec rake import.rake
in the command line.
After running this I get the error:
uninitialised constant Object::Points
I must be missing something which causes this error. What am I doing wrong?