Problem with query and update table on db

Someone can help me please? I posted on stackoverflow here:

My problem is this:

def new
if params[:authorization].present?
      @selected_ids = params[:authorization][:contract_ids]
      @authorizations = Authorization.where("contract_number in (?)", @selected_ids)
      Authorization.where(id: params[:authorization][:contract_ids]).update_all(value_solve: params[:authorization][:value_solve], situation: 2)
    end
    @employee = Employee.search_cpf(params[:search_employee_by_cpf])
    @refinancing = Refinancing.new

Have no more errors, but dont save on my table authorization, and my query is wrong. This is my console:

Started GET "/refinancings/new?utf8=%E2%9C%93&search_employee_by_cpf11111&authorization%5Bcontract_ids%5D%5B%5D=11&authorization%5Bvalue_solve%5D%5B%5D=89888&authorization%5Bvalue_solve%5D%5B%5D=&authorization%5Bvalue_solve%5D%5B%5D=&commit=Reserve" for 127.0.0.1 at 2016-04-18 10:40:08 -0300
Processing by RefinancingsController#new as HTML
  Parameters: {"utf8"=>"✓", "search_employee_by_cpf"=>"1111111111", "authorization"=>{"contract_ids"=>["11"], "value_solve"=>["89888", "", ""]}, "commit"=>"Reserve"}
  SQL (0.2ms)  UPDATE "authorizations" SET "value_solve" = '---
- ''89888''
- ''''
- ''''
', "situation" = 2 WHERE "authorizations"."id" = 11
  Employee Load (0.2ms)  SELECT "employees".* FROM "employees" INNER JOIN "people" ON "people"."id" = "employees"."person_id" WHERE (people.cpf LIKE '%02849112321%')  ORDER BY "employees"."id" ASC LIMIT 1
  Person Load (0.1ms)  SELECT "people".* FROM "people" WHERE "people"."id" = ? LIMIT 1  [["id", 1]]
  Authorization Load (0.2ms)  SELECT "authorizations".* FROM "authorizations" WHERE (contract_number in ('11'))
  Rendered refinancings/new.html.erb within layouts/application (70.5ms)
Completed 200 OK in 119ms (Views: 85.2ms | ActiveRecord: 1.4ms)

How I said, I think that the error is because “authorizations”.id = 11. I haven’t id 11, id 11 equal contract_number. The id was to be 1.

Please, make more than 2 weeks that I try but nothing work =(

Someone can help me please? I posted on stackoverflow here:

ruby on rails - No implicit conversion of Symbol into Integer. A error still no solution for me - Stack Overflow

My problem is this:

def new     if params[:authorization].present?       @selected_ids = params[:authorization][:contract_ids]       @authorizations = Authorization.where("contract_number in (?)", @selected_ids)       Authorization.where(id: params[:authorization][:contract_ids]).update_all(value_solve: params[:authorization][:value_solve], situation: 2)     end     @employee = Employee.search_cpf(params[:search_employee_by_cpf])     @refinancing = Refinancing.new

Have no more errors, but dont save on my table authorization, and my query is wrong. This is my console:

Started GET "/refinancings/new?utf8=%E2%9C%93&search_employee_by_cpf11111&authorization%5Bcontract_ids%5D%5B%5D=11&authorization%5Bvalue_solve%5D%5B%5D=89888&authorization%5Bvalue_solve%5D%5B%5D=&authorization%5Bvalue_solve%5D%5B%5D=&commit=Reserve" for 127.0.0.1 at 2016-04-18 10:40:08 -0300 Processing by RefinancingsController#new as HTML   Parameters: {"utf8"=>"✓", "search_employee_by_cpf"=>"1111111111", "authorization"=>{"contract_ids"=>["11"], "value_solve"=>["89888", "", ""]}, "commit"=>"Reserve"}   SQL (0.2ms) UPDATE "authorizations" SET "value_solve" = '--- - ''89888'' - '''' - '''' ', "situation" = 2 WHERE "authorizations"."id" = 11   Employee Load (0.2ms) SELECT "employees".* FROM "employees" INNER JOIN "people" ON "people"."id" = "employees"."person_id" WHERE (people.cpf LIKE '%02849112321%') ORDER BY "employees"."id" ASC LIMIT 1   Person Load (0.1ms) SELECT "people".* FROM "people" WHERE "people"."id" = ? LIMIT 1 [["id", 1]]   Authorization Load (0.2ms) SELECT "authorizations".* FROM "authorizations" WHERE (contract_number in ('11'))   Rendered refinancings/new.html.erb within layouts/application (70.5ms) Completed 200 OK in 119ms (Views: 85.2ms | ActiveRecord: 1.4ms)

How I said, I think that the error is because "authorizations".id = 11. I haven't id 11, id 11 equal contract_number. The id was to be 1. Please, make more than 2 weeks that I try but nothing work =(

You are making us work too hard by giving too much irrelevant detail. Work out which line is not doing what you expect, then put logger.info(...) statements before that line to display the values of all relevant variables, then if it is still not working come back, show us the line that is failing, the values of all the variables, and the log showing what that line does (if relevant).

Colin

Authorization.where(id: params[:authorization][:contract_ids]).update_all(value_solve: params[:authorization][:value_solve], situation: 2)

This is the line with error, and this is log:

Parameters: {“utf8”=>“✓”, “search_employee_by_cpf”=>“111111111”, “authorization”=>{“contract_ids”=>[“11”, “33”], “value_solve”=>[“34343”, “”, “5454”]}, “commit”=>“Reserve”}

Authorization.where(id: params[:authorization][:contract_ids]).update_all(value_solve: params[:authorization][:value_solve], situation: 2)

This is the line with error, and this is log:

Parameters: {“utf8”=>“✓”, “search_employee_by_cpf”=>“111111111”, “authorization”=>{“contract_ids”=>[“11”, “33”], “value_solve”=>[“34343”, “”, “5454”]}, “commit”=>“Reserve”}

Whenever you have a problem you don’t understand, start by splitting the problem down into smaller parts, so the next thing is to split into two statements and check that the the first part before the update_all gives the correct record set.

authorisations = Authorisations.where(…) And inspect the result.

Colin

My console is this:

Parameters: {“utf8”=>“✓”, “search_employee_by_cpf”=>“11111111111”, “authorization_id”=>“4”, “authorization”=>{“contract_number”=>[“11”], “value_solve”=>[“4343443”, “”, “”, “”]}, “commit”=>“Reserve”}

SQL (315.7ms) UPDATE “authorizations” SET “value_solve” = '—

- ‘‘4343443’’

- ‘’‘’

- ‘’‘’

- ‘’‘’

', “situation” = 2 WHERE “authorizations”.“contract_number” = ‘11’

How do I look like:

Parameters: {“utf8”=>“✓”, “search_employee_by_cpf”=>“11111111111”, “authorization_id”=>“4”, “authorization”=>{“contract_number”=>[“11”], “value_solve”=>[“4343443”, “”, “”, “”]}, “commit”=>“Reserve”}

SQL (315.7ms) UPDATE “authorizations” SET “value_solve” = ‘‘4343443’’, “situation” = 2 WHERE “authorizations”.“contract_number” = ‘11’

You are passing an array for value_solve "value_solve"=>["4343443", "", "", ""]] you just want a single value.

Please quote the relevant parts of the previous message when replying so we don't have to look back through old emails to find what you are talking about. Thanks.

Colin

Damm, my problem continues. I have this:

@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

Where @selected_ids = params[:authorization][:contract_number]

haven’t error, but in console my consult is wrong:

UPDATE “authorizations” SET “value_solve” = '—

  • ‘’‘’
  • ‘‘4345’’
  • ‘‘454’’
  • ‘’‘’ ', “situation” = 2 WHERE “authorizations”.“contract_number” IN (‘22’, ‘33’)

And my column value_solve on Table Authorization is:

What is in params[:authorization][:value_solve]?

Colin

params[:authorization][:value_solve] = contract numbers checked

params[:authorization][:value_solve] = contract numbers checked

No, I meant exactly what data is in it.

Colin

This:

11

33

My contract numbers checked

My contract examples: 11, 22, 33 and 44, respectivaly authorization id 1, 2, 3 and 4

Is that an array? If so then when you say update_all(value_solve: params[:authorization][:value_solve], situation: 2) and params[:authorization][:value_solve] is an array, what do you expect that to do?

Colin

All what I want is this, how show the image. Click on check, fill value, click in Reserve, save on table Authorization.

But now, save, but is wrong, how show the image too.

T__T

It is almost finishing! I believe. Look:

Processing by RefinancingsController#new as HTML Parameters: {“utf8”=>“✓”, “search_employee_by_cpf”=>“11111111111”, “authorization”=>{“contract_number”=>[“11”, “33”], “value_solve”=>[“43343”, “”, “45454”, “”]}, “commit”=>“Reserve”} SQL (91.9ms) UPDATE “authorizations” SET “value_solve” = ‘43343’, “situation” = 2 WHERE “authorizations”.“contract_number” = ? [[“contract_number”, “11”]] SQL (87.0ms) UPDATE “authorizations” SET “value_solve” = ‘’, “situation” = 2 WHERE “authorizations”.“contract_number” = ? [[“contract_number”, “33”]]

The problem is because take just first element, this is the code:

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

I just split in parts

If you look back at my early posts in this thread you will see I did point out that your original code would not work with an array. Apparently you ignored that.

Colin

Yes, 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:

Hey Colin!! Finally!!! FINALLY!!! LOOK:

I just add this:

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

Thanks!! After more then one week and very very very help, I finished it !! * - *

Now… go to next problem LOL hahaha :smiley:

:DDD

Why are you using update_all? You realise it doesn't run ActiveRecord callbacks or validations.

Colin

I didn’t understand… I just needed on my DB and finally worked! What to use instead of upate_all so? I tried update_column and update_columns too, but nothing, because this I chose update_all same = (