我正在尝试将CSV中的一些数据上传到我的数据库(sqlite3,在rails 4中)。我按照列出的here步骤,不仅包括第一条评论中包含的步骤,还包括第二条评论中的替代选项。我正在努力解决NameError:未初始化的常量绘图(我的模型名称)。
我试图通过将config.autoload_paths + =%W(#{config.root} / lib)包含为建议here来修复它,但没有运气。知道如何解决这个问题吗?不希望将CSV文件存储在应用程序中,而是将其行删除并使用其值在我的数据库中创建记录。
LIB /任务/ import_drawings_csv.rake
task :drawing => :environment do
require 'csv'
csv_text = File.read('path/to/public/imageshello.csv')
CSV.foreach("path/to/public/imageshello.csv", :headers => true) do |row|
Drawing.create!(row.to_hash)
end
end
终端追踪
javiers-mbp:colour malditojavi$ rake import_drawings_csv
rake aborted!
NameError: uninitialized constant Drawing
/Users/malditojavi/Desktop/colour/lib/tasks/import_drawings_csv.rake:5:in `block in <top (required)>'
/Users/malditojavi/Desktop/colour/lib/tasks/import_drawings_csv.rake:4:in `<top (required)>'
/Users/malditojavi/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/engine.rb:658:in `load'
/Users/malditojavi/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/engine.rb:658:in `block in run_tasks_blocks'
/Users/malditojavi/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/engine.rb:658:in `each'
/Users/malditojavi/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/engine.rb:658:in `run_tasks_blocks'
/Users/malditojavi/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/application.rb:438:in `run_tasks_blocks'
/Users/malditojavi/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/engine.rb:453:in `load_tasks'
/Users/malditojavi/Desktop/colour/Rakefile:6:in `<top (required)>'
/Users/malditojavi/.rvm/gems/ruby-2.2.0/bin/ruby_executable_hooks:15:in `eval'
/Users/malditojavi/.rvm/gems/ruby-2.2.0/bin/ruby_executable_hooks:15:in `<main>'
(See full trace by running task with --trace)
javiers-mbp:colour malditojavi$ rake import_drawings_csv
application.rb中
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module Colour
class Application < Rails::Application
# new line added for autoloading libs
config.autoload_paths += %W(#{config.root}/lib)
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
# Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true
end
end
答案 0 :(得分:2)
使用以下语法将解决您的问题。
namespace :update_drawing do
desc "Drawing desc"
task :draw_me => :environment do
csv_text = File.read('path/to/public/imageshello.csv')
CSV.foreach('path/to/public/imageshello.csv', :headers => true) do |row|
Drawing.create!(row.to_hash)
end
end
end