繁体   English   中英

使用ruby 1.9.2和rails 3,如何收集activerecord错误以供以后显示? 还是更好,推迟提交直到准备好?

[英]Using ruby 1.9.2 and rails 3, how do I collect activerecord errors to display later? Or better yet, hold off on the commit until ready?

在一次操作中,我将多行插入到表中。 这些行中的一个或多个可能导致ActiveRecord :: RecordInvalid问题。 发生这种情况时,我希望能够撤消该特定操作的所有事务,并让用户在继续操作之前先修复数据。 这样做的最佳方法是什么?

现在,如果第二行数据失败,则第一行数据已经提交到数据库,因此用户将不知道是否应该重新加载第一行。 我可以告诉用户成功的行数是多少,他们可以知道仅从该部分开始进行修复和重新加载,但是如果我可以撤消所有操作并让用户在修复数据后重新开始,那对我来说会更好。

仅供参考,用户最初将CSV文件加载到一个表中,该表包含其csv文件的每一行*每一行的行,并且我从该import_table导入。

这是我的控制器方法的一部分:

  def process_import

    @import = ImportTable.find(params[:id])
    @cells = @import.import_cells
    @new_donors = Array.new
    @existing_donors = Array.new
    class_name = 'Donor'          

    klass = ActiveRecord.const_get(class_name) # get access to class

    #loop through rows
    0.upto(@import.row_count - 1) do |row_index|
      donor = {}
      donation = {} 
      record_status = 'new'     
      row = @cells.select { |cell| cell.row_index == row_index }

      #loop through columns
      0.upto(@import.column_count - 1) do |column_index|
        contents = row.select { |cell| cell.column_index == column_index}[0].contents

        case column_index
          when 0 then   record_status = contents
          when 1 then   donor["id"] = contents
          when 2 then   donor["prefix1"] = contents
          when 3 then   donor["first_name1"] = contents
          when 4 then   donor["middle_name1"] = contents
          ...
        end #case
      end #columns

      unless @donor = Donor.find_by_id(donor["id"])
        donor.delete("id")
        @donor = klass.find_or_initialize_by_company_and_prefix1_and_first_name1_and_last_name1_and_address1(donor) 
      end

      @donor.new_record? ? @new_donors.push(@donor) : @existing_donors.push(@donor)    
      @donor.save!

      if !donation["amount"].blank?
        @donation = @donor.donations.build(donation)
        @donation.save!

        end

    end #rows


   @import.processed_date = Time.now
   @import.save

  end

我只是让用户重新提交整个数据

将导入过程包装在事务中。 如果发生错误,则可以通过引发ActiveRecord::Rollback退出整个提交

klass.transaction do
  # loop through rows
  # You might want to call @donor.save rather than @donor.save! so that you do not raise errors, yet still have invalid records.

  raise ActiveRecord::Rollback if (@new_donors+@existing_donors).any?{|donor| !donor.valid? }
end

暂无
暂无

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

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