Reputation: 171
I've searched similar thread but no luck so far, so I am building a RoR application and I have already create the database, I just want to import the css file into my database, below is the files I have the errors I got:
schema.rb:
ActiveRecord::Schema.define(version: 20170913210926) do
create_table "items", force: :cascade do |t|
t.string "title"
t.string "description"
t.string "author"
t.string "tags"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
I also have a rake task file(import.task):
require 'csv'
csv_text = File.read('/Path/to/csv/Item.csv')
csv = CSV.parse(csv_text, :headers => true)
csv.each do |row|
item.create!(row.to_hash)
end
So when I ran rake import.task in terminal I got the following error:
rake aborted!
NameError: undefined local variable or method `item' for main:Object
Upvotes: 0
Views: 1155
Reputation: 2080
Your rake task looks more like a regular ruby script than a rake task. If you want to use rake for this, try something like:
require 'csv'
namespace :import do
desc 'Imports csv data into db'
task from_csv: :environment do
csv_text = File.read('/Path/to/csv/Item.csv')
csv = CSV.parse(csv_text, :headers => true)
csv.each do |row|
Item.create!(row.to_hash)
end
end
end
and invoke it with rake import:from_csv
Upvotes: 1