Rails 错误未初始化常量导入 csv
Rails error Uninitialized constant importing csv
这是我第一次将 csv 文件导入我的 rails 应用程序。
我在 /lib/tasks/import.rake
中有下面的代码
require 'csv'
CSV.foreach("lib/articles.csv", headers: true, encoding: "ISO8859-1") do |row|
Article.new(title: row["Title"], body: row["Body"], user: User.find(1))
end
当我运行rake import:articles
我收到这个错误:
NameError: uninitialized constant Article
/Users/justinMgrant/code/hrsurvival/lib/tasks/import.rake:8:in `block in <top (required)>'
/Users/justinMgrant/code/hrsurvival/lib/tasks/import.rake:7:in `<top (required)>'
/Users/justinMgrant/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/engine.rb:658:in `block in run_tasks_blocks'
/Users/justinMgrant/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/engine.rb:658:in `each'
/Users/justinMgrant/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/engine.rb:658:in `run_tasks_blocks'
/Users/justinMgrant/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/application.rb:452:in `run_tasks_blocks'
/Users/justinMgrant/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/engine.rb:453:in `load_tasks'
/Users/justinMgrant/code/hrsurvival/Rakefile:6:in `<top (required)>'
(See full trace by running task with --trace)
知道我做错了什么吗?
问题是您实际上并没有在 rakefile 中定义您的任务。这应该对你有用 运行 rake import:articles
.
namespace :import do
desc 'An optional description for what the task does'
task :articles => :environment do
# your code goes here
end
end
rake import:articles
是说要在名为 import
的命名空间中查找名为 articles
的任务,这就是为什么命名空间对于您当前正在尝试的内容是必需的。
正如@max 提到的,利用 task :articles => :environment
是告诉任务在 Rails 环境的上下文中 运行,这将使您的 Articles
模型以及您在该任务中可用的任何其他模型。
这是我第一次将 csv 文件导入我的 rails 应用程序。
我在 /lib/tasks/import.rake
中有下面的代码 require 'csv'
CSV.foreach("lib/articles.csv", headers: true, encoding: "ISO8859-1") do |row|
Article.new(title: row["Title"], body: row["Body"], user: User.find(1))
end
当我运行rake import:articles
我收到这个错误:
NameError: uninitialized constant Article
/Users/justinMgrant/code/hrsurvival/lib/tasks/import.rake:8:in `block in <top (required)>'
/Users/justinMgrant/code/hrsurvival/lib/tasks/import.rake:7:in `<top (required)>'
/Users/justinMgrant/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/engine.rb:658:in `block in run_tasks_blocks'
/Users/justinMgrant/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/engine.rb:658:in `each'
/Users/justinMgrant/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/engine.rb:658:in `run_tasks_blocks'
/Users/justinMgrant/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/application.rb:452:in `run_tasks_blocks'
/Users/justinMgrant/.rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/engine.rb:453:in `load_tasks'
/Users/justinMgrant/code/hrsurvival/Rakefile:6:in `<top (required)>'
(See full trace by running task with --trace)
知道我做错了什么吗?
问题是您实际上并没有在 rakefile 中定义您的任务。这应该对你有用 运行 rake import:articles
.
namespace :import do
desc 'An optional description for what the task does'
task :articles => :environment do
# your code goes here
end
end
rake import:articles
是说要在名为 import
的命名空间中查找名为 articles
的任务,这就是为什么命名空间对于您当前正在尝试的内容是必需的。
正如@max 提到的,利用 task :articles => :environment
是告诉任务在 Rails 环境的上下文中 运行,这将使您的 Articles
模型以及您在该任务中可用的任何其他模型。