Ruby on rails 如何组合未更新记录的集合

Ruby on rails 如何组合未更新记录的集合,ruby-on-rails,ruby,Ruby On Rails,Ruby,我也有同样的问题,不知道如何去实现它的想法。这是我的密码: controller.rb def更新 开始 dot_applications=DotApplication.where(id:params[:id]) 坏点应用程序更新=[] dot|u应用程序。每个dot|u应用程序| 如果dot_application.update!(lead_source_id:resource_params[:lead_source_id]) dot_application.update!(lead_sour

我也有同样的问题,不知道如何去实现它的想法。这是我的密码:

controller.rb
def更新
开始
dot_applications=DotApplication.where(id:params[:id])
坏点应用程序更新=[]
dot|u应用程序。每个dot|u应用程序|
如果dot_application.update!(lead_source_id:resource_params[:lead_source_id])
dot_application.update!(lead_source_id:resource_params[:lead_source_id])
其他的
坏点应用程序更新错误
“渲染”对话框:{
关闭对话框:true,
错误消息:“无法更新潜在客户源,因为{error.message}”
}
结束
结束

我想收集未更新的“dot_应用程序”,将其添加到阵列中,并将其插入以拯救标准错误。请帮帮我。

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

通过在块内调用,根据记录的保存状态对记录进行分组,可以实现所需的结果:

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

您应该使用
update
而不是
update
因为捕捉到异常的那一分钟,您的代码执行将停止并重定向到rescue块<另一方面,代码>更新仅在失败时返回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

你有什么错误吗?无论如何,请检查这是否有帮助:我更新了控制器。但我不确定它的工作是否正确。您当前的代码将在第一次
更新时“中止”失败-因此,现在还没有必要尝试“向数组添加错误”(只能有一个错误)。如果某些
dot应用程序
s无法更新,您希望采取什么行为?没有更新吗?或者更新其中的一些?
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