选中多个复选框后,在数据库中保存数组

时间:2016-04-20 12:20:18

标签: sql ruby-on-rails ruby ruby-on-rails-4 checkbox

我有一个最大的问题!!我想这真的想要Array,当我只检查一个时,看看我的控制台:

{"value_solve"=>["", "", "333", ""], "contract_number"=>["33"]}
-----
  SQL (317.5ms)  UPDATE "authorizations" SET "value_solve" = '', "situation" = 2 WHERE "authorizations"."contract_number" = ?  [["contract_number", "33"]]

之后,当我只检查一个时,第一个:

{"value_solve"=>["111", "", "", ""], "contract_number"=>["11"]}
-----
  SQL (317.5ms)  UPDATE "authorizations" SET "value_solve" =  '111 ', "situation" = 2 WHERE "authorizations"."contract_number" = ?  [["contract_number", "11"]]

而且,最后,当我只有一个时:

{"contract_number"=>["11", "44"], "value_solve"=>["111", "", "", "444"]}
-----
  SQL (297.7ms)  UPDATE "authorizations" SET "value_solve" = '111', "situation" = 2 WHERE "authorizations"."contract_number" = ?  [["contract_number", "11"]]
  SQL (121.9ms)  UPDATE "authorizations" SET "value_solve" = '', "situation" = 2 WHERE "authorizations"."contract_number" = ?  [["contract_number", "44"]]

这是我的控制者:

@selected_ids = params[:authorization][:contract_number]
  @authorizations = Authorization.where("contract_number in (?)", @selected_ids)
  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

只需将第一个值保存在DB上,如何保存多个值?谢谢!

1 个答案:

答案 0 :(得分:1)

据我了解,您希望身份contract_number的{​​{1}}与44“关联”。如果这是正确的,您应该从value_solve == "444"数组中删除空格:

value_solve

现在auth_params[:contract_number].zip(auth_params[:value_solve].reject(&:blank?))... 正在使用44的第二个元素进行更新,这显然是一个空字符串。

有关详细信息,请参阅Array#zip