Elton Santos - 10 months ago 19

Ruby Question

I have a problem greatest!! I guess that really want Array, look my console when I checked just one:

`{"value_solve"=>["", "", "333", ""], "contract_number"=>["33"]}`

-----

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

After, when I checked just one, the first:

`{"value_solve"=>["111", "", "", ""], "contract_number"=>["11"]}`

-----

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

And, for last, when I just more then one:

`{"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"]]

And this is my controller:

`@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

Just save the first value on DB, how I can save more then one value? Thanks!

Answer

As I understood, you want the `contract_number`

with id `44`

to be “associated” with `value_solve == "444"`

. If this is correct, you should remove blanks from your `value_solve`

array:

```
auth_params[:contract_number].zip(auth_params[:value_solve].reject(&:blank?))...
```

Now `44`

is being updated with the second element of `value_solve`

, which is apparently an empty string.

See `Array#zip`

for more details.