我有这段代码:
@selected_ids = params[:authorization][:contract_number]
@selected_ids.zip(params[:authorization][:contract_number]).each do |id, value|
Authorization.where(contract_number: params[:authorization][:contract_number]).update_all(value_solve: params[:authorization][:value_solve], situation: 2)
end
避免错误,但在我的控制台中生成查询
Processing by RefinancingsController#new as HTML
Parameters: {"utf8"=>"✓", "search_employee_by_cpf"=>"11111111111", "authorization"=>{"value_solve"=>["", "4345", "454", ""], "contract_number"=>["22", "33"]}, "commit"=>"Reserve"}
SQL (344.2ms) UPDATE "authorizations" SET "value_solve" = '---
- ''''
- ''4345''
- ''454''
- ''''
', "situation" = 2 WHERE "authorizations"."contract_number" IN ('22', '33')
SQL (133.1ms) UPDATE "authorizations" SET "value_solve" = '---
- ''''
- ''4345''
- ''454''
- ''''
', "situation" = 2 WHERE "authorizations"."contract_number" IN ('22', '33')
在我的数据库中,每个选中的复选框都会重复这些值。
此示例为列value_solve,授权标识2
---
- ''
- '4345'
- '454'
- ''
列value_solve,授权标识3
---
- ''
- '4345'
- '454'
- ''
请,就这一点,我如何解决此问题并在数据库中正确保存? 我需要这个:
SQL (344.2ms) UPDATE "authorizations" SET "value_solve" = "4345", "situation" = 2 WHERE "authorizations"."contract_number" IN '22'
SQL (133.1ms) UPDATE "authorizations" SET "value_solve" = "454", "situation" = 2 WHERE "authorizations"."contract_number" IN '33'
答案 0 :(得分:0)
您需要将其分解为两个更新,而不是一个具有数组值的更新:
auth_params = params[:authorization]
auth_params[:contract_number].zip(auth_params[:value_solve]).each do |contract_number, value_solve|
Authorization.where(contract_number: contract_number).update_all(value_solve: value_solve, situation: 2)
end
这应该使用正确的参数触发两个更新。
答案 1 :(得分:0)
答案是:
auth_params = params[:authorization]
auth_params[:contract_number].zip(auth_params[:value_solve].reject(&:blank?)).each do |contract_number, value_solve|
Authorization.where(contract_number: contract_number).update_all(value_solve: value_solve, situation: 2)
end
:d