【问题标题】:Reading values into an array将值读入数组
【发布时间】:2012-09-19 08:36:03
【问题描述】:

我正在尝试以最简单的方式将一些值读入 .each 代码块中的数组,但我遇到了问题。我正在通过我构建的 rake 文件导入 csv 文件,该文件将使用新行更新表,从而创建新的 id。我正在尝试获取创建的每个 id 并将它们保存到一个数组中。而且我希望能够在另一个 rake 任务中访问存储在该数组中的值。

以下代码用于将导入 csv 文件的 rake 文件,并且我希望将其新生成的 id 捕获到一个数组中。它目前完成了它应该做的事情,但您一次只能导入一个新行。

Import_incidents_csv.rake

require 'csv'

namespace :import_incidents_csv do

task :create_incidents => :environment do
    puts "Import Incidents"

    csv_text = File.read('c:/rails/thumb/costrecovery_csv/lib/csv_import/incidents.csv')
    csv = CSV.parse(csv_text, :headers => true)
    csv.each do |row|
        row = row.to_hash.with_indifferent_access
        Incident.create!(row.to_hash.symbolize_keys)
    end

    @last_incident_id = Incident.last.id

    end
  end

这是另一个 rake 文件,它导入另一个 csv 文件,我需要将值存储在分配给的数组中。再说一次,如果你只导入一个新行,目前它工作正常,但如果你导入多行,那么一切都会变得有点混乱。

Import_timesheets_csv.rake

require 'csv'

namespace :import_timesheets_csv do

 task :create_timesheets => :environment do
    puts "Import Timesheets"

    csv_text = File.read('c:/rails/thumb/costrecovery_csv/lib/csv_import/timesheets.csv')
    csv = CSV.parse(csv_text, :headers => true)
    csv.each do |row|
        row = row.to_hash.with_indifferent_access
        Timesheet.create!(row.to_hash.symbolize_keys)
    end

    timesheet = Timesheet.last
    timesheet.incident_id = @last_incident_id
    timesheet.save

    @last_timesheet_id = Timesheet.last.id

     end
end

我阅读了处理数组http://www.tutorialspoint.com/ruby/ruby_arrays.htm 的此资源,它似乎很混乱。这是我对 Import_incidents_csv.rake 文件在将值读入数组时的最佳猜测。最后我有puts,所以我可以验证整数是否正确存储在数组中。一切正常后,我将删除它。

require 'csv'

def inc_id
@inc_id = Incident.last.id
end

namespace :import_incidents_csv do

 task :create_incidents => :environment do
    puts "Import Incidents"

    csv_text = File.read('c:/rails/thumb/costrecovery_csv/lib/csv_import/incidents.csv')
    csv = CSV.parse(csv_text, :headers => true)
    csv.each do |row|
        row = row.to_hash.with_indifferent_access
        Incident.create!(row.to_hash.symbolize_keys)
        Incident.last.id = @inc_id
        id_array = Array.new(@inc_id)
    end

    puts "#{id_array}"

      end
  end

【问题讨论】:

    标签: ruby-on-rails ruby arrays ruby-on-rails-3 csv


    【解决方案1】:

    我不确定您要在这里做什么,您的问题还不清楚,但可能是您正在尝试将插入记录的 id 值收集到一个数组中:

    id_array = [ ]
    
    csv.each do |row|
      incident = Incident.create!(row.to_hash.symbolize_keys)
      id_array << incident.id
    end
    

    一旦完成,id_array 将在其中包含所有创建的记录 id 值。请注意,如果这些create! 调用中的任何一个失败,您将获得ActiveRecord::RecordInvalid 异常,您需要以某种方式抢救和处理该异常。因此,将整个操作包装在Indcident.transaction do ... end 中是有意义的,以确保在失败时可以回滚整个操作,假设您的数据库支持事务。如果你不关心失败,你可以调用create,它不会抛出异常。

    您的示例中有很多冗余,已被省略。几乎不需要调用 Array.new,因为就像 JavaScript 一样,您可以声明一个带有 [ ] 的新数组,可以是空的,也可以预先填充有 [ 1 ][ 1, 2 ] 之类的东西。

    【讨论】:

    • 您完美地回答了我的问题,谢谢。我已经将事件 ID 存储在一个全局数组中,我可以在另一个 rake 文件中访问该数组。那么现在我如何将该数组的内容清空到一个名为 event_id 的列中,直到该数组为空。这是一个猜测: Timesheet.create!(row.to_hash.symbolize_keys) (newline) timesheet = Timesheet.last (newline) timesheet.incident_id
    • 我不确定您对timesheet.incident_id &lt;&lt; @id_array 的意图是什么,因为您可能打算扭转这些情况。 &lt;&lt; 用于以array &lt;&lt; item 的形式将事物添加到数组中。与 UNIX shell 重定向一样,将尖括号视为漏斗,您将在其中将内容汇集到 数组中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-28
    • 2011-08-10
    • 2015-06-10
    • 2011-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多