2015-01-02 233 views
1

我想從CSV上傳一些數據到我的數據庫(sqlite3,在rails 4中)。我遵循here列出的步驟,不僅包括第一條評論中包含的步驟,還包含第二條評論中的替代選項。我努力與NameError:未初始化的常量繪圖(我的模型名稱)。NameError:未初始化的常量ModelName

我試圖通過包含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 

回答

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 
相關問題