NameError:未定义的局部变量或方法`table'for main:Object

时间:2016-01-04 15:26:40

标签: ruby-on-rails csv

我正在尝试设置一个csv url,以便在首先截断数据库之后将数据从csv保存到我的数据库中。

然而,当我为它运行耙子时,它似乎在此处黯然失色。

LoadError:无法自动加载常量Gigantictable,预期/Users/samroberts/Desktop/project/app/models/gigantictable.rb来定义它

现在我的模型完全是空的,我猜,我应该在里面有东西。

require 'open-uri'
namespace :populate do
   task reload: :environment do
       Gigantictable.delete_all
       url = "Private url here"
       CSV.open(url).each do |row|
         Gigantictable.create(name: row[0], address: row[1])
       end
   end
end

我在我的数据库中命名了表gigantictable,第一行中的所有标题都作为数据库中的标题(不包括数据库中第一列的id)

任何帮助都很可爱!

所以我发现了一个错误,我把它当作类Gigantictables nto table。

此错误现在是主要错误

bundle exec rake populate:reload
rake aborted!
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "gigantictables" does not exist
LINE 1: DELETE FROM "gigantictables"
                    ^
: DELETE FROM "gigantictables"
/Users/samroberts/Desktop/project/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `async_exec'
/Users/samroberts/Desktop/project/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `block in exec_no_cache'
/Users/samroberts/Desktop/project/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract_adapter.rb:472:in `block in log'
/Users/samroberts/Desktop/project/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/Users/samroberts/Desktop/project/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract_adapter.rb:466:in `log'
/Users/samroberts/Desktop/project/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `exec_no_cache'
/Users/samroberts/Desktop/project/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:584:in `execute_and_clear'
/Users/samroberts/Desktop/project/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql/database_statements.rb:173:in `exec_delete'
/Users/samroberts/Desktop/project/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/database_statements.rb:119:in `delete'
/Users/samroberts/Desktop/project/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `delete'
/Users/samroberts/Desktop/project/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/relation.rb:478:in `delete_all'
/Users/samroberts/Desktop/project/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/querying.rb:8:in `delete_all'
/Users/samroberts/Desktop/project/lib/tasks/populate.rake:4:in `block (2 levels) in <top (required)>'
PG::UndefinedTable: ERROR:  relation "gigantictables" does not exist
LINE 1: DELETE FROM "gigantictables"
                    ^
/Users/samroberts/Desktop/project/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `async_exec'
/Users/samroberts/Desktop/project/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `block in exec_no_cache'
/Users/samroberts/Desktop/project/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract_adapter.rb:472:in `block in log'
/Users/samroberts/Desktop/project/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/Users/samroberts/Desktop/project/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract_adapter.rb:466:in `log'
/Users/samroberts/Desktop/project/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `exec_no_cache'
/Users/samroberts/Desktop/project/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:584:in `execute_and_clear'
/Users/samroberts/Desktop/project/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql/database_statements.rb:173:in `exec_delete'
/Users/samroberts/Desktop/project/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/database_statements.rb:119:in `delete'
/Users/samroberts/Desktop/project/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `delete'
/Users/samroberts/Desktop/project/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/relation.rb:478:in `delete_all'
/Users/samroberts/Desktop/project/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/querying.rb:8:in `delete_all'
/Users/samroberts/Desktop/project/lib/tasks/populate.rake:4:in `block (2 levels) in <top (required)>'
Tasks: TOP => populate:reload
(See full trace by running task with --trace)

萨姆

1 个答案:

答案 0 :(得分:0)

您可能还需要: require 'csv' 在文件的顶部。
请考虑查看this