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 -0300Processing 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 =(

On 18 April 2016 at 19:21, Elton S. [email protected]
wrote:

@selected_ids)

  • ‘’‘’

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”}

On 19 Apr 2016 3:16 a.m., “Elton S.” [email protected]
wrote:

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


You received this message because you are subscribed to the Google G.
“Ruby on Rails: Talk” group.
To unsubscribe from this group and stop receiving emails from it, send an
email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/b1b62337-bda4-4362-b359-de4d56e50c0c%40googlegroups.com
.

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’

On 19 April 2016 at 14:02, Elton S. [email protected]
wrote:

', “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:

- ‘’

- ‘4345’

- ‘454’

- ‘’

for each line checked this value repeat =(

=/

On 19 April 2016 at 19:53, Elton S. [email protected]
wrote:

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’)

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

Colin

On 19 April 2016 at 20:56, Elton S. [email protected]
wrote:

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

No, I meant exactly what data is in it.

Colin

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

This:

11
33

My contract numbers checked

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

On 19 April 2016 at 21:30, Elton S. [email protected]
wrote:

This:

11
33

My contract numbers checked

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.

https://lh3.googleusercontent.com/-J3y6Vo-YmSk/VxahAfHUTJI/AAAAAAAAD3A/UbuRsiaGxowDyGEDExwGEMzTTkmzv8uJwCLcB/s1600/justthis.png
But now, save, but is wrong, how show the image too.

https://lh3.googleusercontent.com/-fgk8OIU6Has/VxahEBcg2LI/AAAAAAAAD3E/qctgTg8V7u4qSGX9dDBK72a9P6vPK3tyQCLcB/s1600/dammm.png

T__T

On 20 April 2016 at 02:54, Elton S. [email protected]
wrote:

“situation” = 2 WHERE “authorizations”.“contract_number” = ?
end

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

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

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:

{“contract_number”=>[“11”, “44”], “value_solve”=>[“111”, “”, “”, “444”]}

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

On 20 April 2016 at 13:46, Elton S. [email protected]
wrote:

  end

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

Colin

On 20 April 2016 at 14:26, Elton S. [email protected]
wrote:

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 = (

Once again you have not quoted the previous reply, leaving it for me
to guess what it is you did not understand.

Usually you would update the fields in the record and then save it.
Then you can check it saved ok in case validations failed. Look at
the code generally used to update a record in the controller.

Colin