2

I'm trying to upload some data from a CSV into my db (sqlite3, in rails 4). I followed the steps listed here, not only the ones included in the first comment, but also the alternative option in the 2nd comment. I'm struggling with a NameError: uninitialized constant Drawing (my Model name).

I have tried to fix it by including config.autoload_paths += %W(#{config.root}/lib) as adviced here, but no luck. Any idea how to tackle this issue? Don't want to store the CSV file within the app, but exctract its rows and create records in my DB with their values.

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

terminal trace

            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
Community
  • 1
  • 1
malditojavi
  • 1,074
  • 2
  • 14
  • 28

1 Answers1

2

Using below syntax will fix your issue.

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
Roman
  • 19,581
  • 6
  • 68
  • 84
Ranjan Kumar
  • 49
  • 1
  • 8