【问题标题】:NameError: uninitialized constant ModelNameNameError:未初始化的常量 ModelName
【发布时间】:2015-03-01 02:33:29
【问题描述】:

我正在尝试将一些数据从 CSV 上传到我的数据库(sqlite3,在 rails 4 中)。我按照here 列出的步骤进行操作,不仅是第一条评论中包含的步骤,还包括第二条评论中的替代选项。我正在努力解决 NameError: uninitialized constant Drawing (my Model name)。

我已尝试通过包含 config.autoload_paths += %W(#{config.root}/lib) 的建议来修复它here,但没有运气。知道如何解决这个问题吗?不想将 CSV 文件存储在应用程序中,而是提取其行并使用它们的值在我的数据库中创建记录。

lib/tasks/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

应用程序.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

【问题讨论】:

    标签: ruby-on-rails-4 rake


    【解决方案1】:

    使用以下语法将解决您的问题。

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-19
      • 2015-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-04
      • 2016-06-12
      • 2018-06-26
      相关资源
      最近更新 更多