【问题标题】:How to assemble a collection of not updated records如何组装未更新记录的集合
【发布时间】:2019-02-05 14:05:45
【问题描述】:

我有同样的问题,不明白如何实现它的想法。这是我的代码:

controller.rb

def update
  begin
    dot_applications = DotApplication.where(id: params[:ids])
    bad_dot_app_update = []
    dot_applications.each do |dot_application|
      if dot_application.update!(lead_source_id: resource_params[:lead_source_id])
        dot_application.update!(lead_source_id: resource_params[:lead_source_id])
      else
        bad_dot_app_update << dot_application.update!(lead_source_id: resource_params[:lead_source_id])
      end
    end

    render dialog: {
      close_dialog: true,
      success_message: "The lead source was changed for selected applicants."
    }
  rescue StandardError => error
    render dialog: {
      close_dialog: true,
      error_message: "Can't update lead sources because #{error.message}"
    }
  end
end

我想收集未更新的 'dot_aplication' 将其添加到数组并将其插入以挽救 StandartError。请帮帮我。

【问题讨论】:

  • 您是否遇到任何错误?无论如何,检查这是否有帮助:stackoverflow.com/questions/3694153/…
  • 我更新了控制器。但我不确定它的工作是否正确。
  • update! 第一次失败时,您当前的代码将“中止” - 所以尝试“向数组添加错误”是没有意义的(只能有 1 个错误)。如果某些DotApplications 无法更新,您想要什么行为?一个都不更新?或者更新其中的一些?

标签: ruby-on-rails ruby


【解决方案1】:

您应该使用update 而不是update!,因为一旦捕获到异常,您的代码执行就会暂停并重定向到救援块。另一方面,update 仅在失败时返回 false。

试试这样的:

def update

  dot_applications = DotApplication.where(id: params[:ids])
  bad_dot_app_update = dot_applications.reject do |dot_app| # Reject element where updates are successfull
    dot_application.update(lead_source_id: resource_params[:lead_source_id])        
  end

  if bad_dot_app_update.any?
    # Here you can access the array and do whatever you want.
    errors = bad_dot_app_update.collect {|bad_dot_app| bad_dot_app.errors }
    # Collecting errors, only needs formating for printing
    render dialog: {
      close_dialog: true,
      error_message: "Can't update lead sources because #{error.message}"
    }
  else
    render dialog: {
      close_dialog: true,
      success_message: "The lead source was changed for selected applicants."
    }
  end
end

【讨论】:

    【解决方案2】:

    这很简单。如果您调用 #update! 而不是 #update 代码将在第一条记录失败时引发异常,跳转到救援块(从而停止进一步更新记录)。

    您可以通过在#group_by 块内调用#update 来实现您想要的结果,并根据它们的保存状态对记录进行分组:

    dot_applications = DotApplication.where(id: params[:ids])
    saved = dot_applications.group_by do |dot_application|
      dot_application.update(lead_source_id: resource_params[:lead_source_id])
    end
    
    # Saved records will be in `saved[true]` unsaved records will be in
    # `saved[false]`. If all records saved `saved[false]` will be `nil`. If none of
    # the records saved `saved[true]` will be `nil`.
    
    if saved[false]
      # some (or all) records didn't save
      unsaved_ids = saved[false].pluck(:id).join(', ')
      render dialog: {
        close_dialog: true,
        error_message: "The following selected applicants didn't save: #{unsaved_ids}"
      }
    else
      # success
      render dialog: {
        close_dialog: true,
        success_message: "The lead source was changed for selected applicants."
      }
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-01
      • 2020-12-29
      • 1970-01-01
      相关资源
      最近更新 更多