简体   繁体   English

Rake任务不将CSV添加到数据库

[英]Rake task not adding CSV to database

I'm trying to add rows of a CSV to records in a table and everything seems to be working fine besides the fact that nothing actually shows up. 我正在尝试将CS​​V行添加到表中的记录中,除了事实上没有任何实际显示之外,一切似乎都正常。

Here is my Rake task: 这是我的Rake任务:

namespace :csv do

  desc "Import CSV Data"
  task :c_up => :environment do

    require 'csv'

    csv_file_path = 'db/main.csv'

    CSV.foreach(csv_file_path, :headers => true) do |row|
      row = Entry.create!({
        :one => row[0],
        :two => row[1],
        :three => row[2],
        :four => row[3],
        :five =>row[4],
        :six => row[5],
        :seven => row[6],
        :eight => row[7],
        :nine => row[8],
        :ten => row[9]
      })
      row.save
      puts "Entry added!"

    end
  end
end

The headers in my CSV are named to match the attributes. 我的CSV中的标题被命名为与属性匹配。

When I run the task I receive no errors and all of the "Entry added!" 当我运行任务时,我没有收到任何错误,并且所有“条目已添加!” show up in the console. 出现在控制台中。 I've tried it without the row = and row.save as I've seen lots of examples without those but it doesn't help. 我已经尝试过没有row =row.save因为我看过很多没有这些的例子,但它没有帮助。

After the rake finishes there aren't any records at all. 耙子完成后根本没有任何记录。 Is there something obvious I'm doing wrong? 有什么明显的东西我做错了吗? It seems like this is following most examples I've seen. 看来这是我见过的大多数例子。

Edit Thanks for all your answers and comments! 编辑感谢您的所有答案和评论! Obviously this is flawed all the way down to syntax, I'm going to try out your suggestions and I'll update this after. 显然这一直是语法上的缺陷,我将尝试你的建议,我会在之后更新。 Thanks again! 再次感谢!

Edit 2 It turned out to be a syntax issue that you guys all pointed out, so +1 for everyone thanks! 编辑2结果证明这是一个你们都指出的语法问题,所以给大家+1,谢谢! The reason when I commented it still wasn't working was an unrelated problem which I wouldn't have figured out without @jvnill's comments. 我评论它仍然没有工作的原因是一个无关的问题,如果没有@jvnill的评论,我不会想到这个问题。

Entry.create! does not require being assigned to row nor does it require a row.save . 不需要分配给行也不需要row.save Also know '{}' Just do: 也知道'{}'只是做:

Entry.create!(
        :one => row[0],
        :two => row[1],
        :three => row[2],
        :four => row[3],
        :five =>row[4],
        :six => row[5],
        :seven => row[6],
        :eight => row[7],
        :nine => row[8],
        :ten => row[9]
      )

The create method will populate the row and save it as well. create方法将填充行并保存它。

try with: 尝试:

row = Entry.new(
        :one => row[0],
        :two => row[1],
        :three => row[2],
        :four => row[3],
        :five =>row[4],
        :six => row[5],
        :seven => row[6],
        :eight => row[7],
        :nine => row[8],
        :ten => row[9]
      )
      row.save!

Without { } , and with save! 没有{} ,并且保存!

You could even try with this, to make it simpler: 您甚至可以尝试使用它,使其更简单:

Entry.create!(row.to_hash)

namespace :csv do 命名空间:csv do

desc "Import CSV Data" task :c_up => :environment do desc“导入CSV数据”任务:c_up =>:环境做

require 'csv'

csv_file_path = 'db/main.csv'

CSV.foreach(csv_file_path, :headers => true) do |row|
  Entry.create!({
    :one => row[0],
    :two => row[1],
    :three => row[2],
    :four => row[3],
    :five =>row[4],
    :six => row[5],
    :seven => row[6],
    :eight => row[7],
    :nine => row[8],
    :ten => row[9]
  })
end

end end 结束

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

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