简体   繁体   English

使用CSV文件导入数据,不知道如何构建任务'csv:import_movies'

[英]Import data with CSV file, Don't know how to build task 'csv:import_movies'

I am trying to populate my database with a CSV file. 我正在尝试用CSV文件填充数据库。 I have looked for other questions on SO and after trying everything I am stuck and was able come up with this: 我已经在SO上寻找其他问题,并且在尝试了所有问题之后,终于想到了:

# lib/tasks/import.rake
require 'csv'

desc 'Import Movies'
namespace :csv do
  task :import_movies => [:environment] do
    csv_file = '#{Rails.root}/lib/data/movie.csv'

    CSV.foreach(csv_file, headers: true) do |row|
      Movie.create! ({
        name: row[0],
        url: row[1],
        year: row[2],
        description: row[3],
        genre: row[4]
        })
    end

    # this was also a method others were recommending so I saved it
    #CSV.foreach(filename, :headers => true) do |row|
      #Movie.create!(row.to_hash)
    #end

  end
end

But when I run rake csv:import_movies I get this error running rake csv:import_movies --trace 但是当我运行rake csv:import_movies此错误运行rake csv:import_movies --trace

rake aborted!                                                                                   
Don't know how to build task  'csv:import_movies'                                                                        
/home/action/.rvm/gems/ruby-2.1.1/gems/rake-10.3.1/lib/rake/task_manager.rb:62:in`[]'                                                                 
/home/action/.rvm/gems/ruby-2.1.1/gems/rake-10.3.1/lib/rake/application.rb:149:in `invoke_task'                                                                              
/home/action/.rvm/gems/ruby-2.1.1/gems/rake-10.3.1/lib/rake/application.rb:106:in `block (2 levels) in top_level'                                                            
/home/action/.rvm/gems/ruby-2.1.1/gems/rake-10.3.1/lib/rake/application.rb:106:in `each'                                                                                     
/home/action/.rvm/gems/ruby-2.1.1/gems/rake-10.3.1/lib/rake/application.rb:106:in `block in top_level'                                                                       
/home/action/.rvm/gems/ruby-2.1.1/gems/rake-10.3.1/lib/rake/application.rb:115:in `run_with_threads'                                                                         
/home/action/.rvm/gems/ruby-2.1.1/gems/rake-10.3.1/lib/rake/application.rb:100:in `top_level'                                                                                
/home/action/.rvm/gems/ruby-2.1.1/gems/rake-10.3.1/lib/rake/application.rb:78:in `block in run'                                                                              
/home/action/.rvm/gems/ruby-2.1.1/gems/rake-10.3.1/lib/rake/application.rb:176:in `standard_exception_handling'                                                              
/home/action/.rvm/gems/ruby-2.1.1/gems/rake-10.3.1/lib/rake/application.rb:75:in `run'                                                                                      
/home/action/.rvm/gems/ruby-2.1.1/gems/rake-10.3.1/bin/rake:33:in `<top(required)>'             
/home/action/.rvm/gems/ruby-2.1.1/bin/rake:23:in `load'                                                                                     
/home/action/.rvm/gems/ruby-2.1.1/bin/rake:23:in `<main>'                                                                                   
/home/action/.rvm/gems/ruby-2.1.1/bin/ruby_executable_hooks:15:in `eval'                                                                                     
/home/action/.rvm/gems/ruby-2.1.1/bin/ruby_executable_hooks:15:in `<main>'

And I also tried doing it using this method 我也尝试使用这种方法

# db/seeds.rb
require 'csv'

csv_file_path = '#{Rails.root}/lib/data/movie.csv'
File.open(csv_file_path) do |movies|
  movies.read.each_line do |movie|
    title, url, year, description, genre = movie.chomp.split(",")
    Movie.create!( name: title, url: url, year: year, description: decription, genre: genre )           
  end
end

And then ran rake db:seed --trace and then I got an error again which was: 然后运行rake db:seed --trace ,然后又出现了一个错误:

** Invoke db:seed (first_time)                                                                               
** Execute db:seed                                                                                    
** Invoke db:abort_if_pending_migrations (first_time)                                                                               
** Invoke environment (first_time)                                                                               
** Execute environment                                                                                
** Execute db:abort_if_pending_migrations

I ran rake db:migrate:status and got this, which is weird and I haven't come across before: 我运行rake db:migrate:status并得到了它,这很奇怪,而且我之前从未见过:

Status   Migration ID    Migration Name                                                                                       
--------------------------------------------------                                         
up     20140407094349  ********** NO FILE **********                                                                                 
up     20140407094355  ********** NO FILE **********                                                                                 
up     20140407094405  ********** NO FILE **********                                                                                 
up     20140409112119  ********** NO FILE **********                                                                                 
up     20140409113144  ********** NO FILE **********

I hope someone has experience with this as this is my first time attempting to use rake and csv files in Rails. 我希望有人对此有所了解,因为这是我第一次尝试在Rails中使用rake和csv文件。 Thanks in advance! 提前致谢!

try renaming your rake file to - 'csv.rake' and remove the [] around :environment 尝试将rake文件重命名为-'csv.rake'并删除:environment周围的[]

also put the description inside of the :namespace 也把描述放在:namespace里面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM