Forum: Ruby on Rails Problem with query and update table on db

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2e903d3512efa714af85ed919b2ece46?d=identicon&s=25 Elton Santos (Guest)
on 2016-04-18 20:23
(Received via mailing list)
Someone can help me please? I posted on stackoverflow here:

http://stackoverflow.com/questions/36694536/no-imp...

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 =(
4c6bde00168d595053c09aac7e487f8e?d=identicon&s=25 Colin Law (Guest)
on 2016-04-18 21:59
(Received via mailing list)
On 18 April 2016 at 19:21, Elton Santos <elton.melo.santos@gmail.com>
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
2e903d3512efa714af85ed919b2ece46?d=identicon&s=25 Elton Santos (Guest)
on 2016-04-19 04:17
(Received via mailing list)
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"}
4c6bde00168d595053c09aac7e487f8e?d=identicon&s=25 Colin Law (Guest)
on 2016-04-19 08:05
(Received via mailing list)
On 19 Apr 2016 3:16 a.m., "Elton Santos" <elton.melo.santos@gmail.com>
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 Groups
"Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
email to rubyonrails-talk+unsubscribe@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk...
.
2e903d3512efa714af85ed919b2ece46?d=identicon&s=25 Elton Santos (Guest)
on 2016-04-19 15:03
(Received via mailing list)
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'*
4c6bde00168d595053c09aac7e487f8e?d=identicon&s=25 Colin Law (Guest)
on 2016-04-19 16:13
(Received via mailing list)
On 19 April 2016 at 14:02, Elton Santos <elton.melo.santos@gmail.com>
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
2e903d3512efa714af85ed919b2ece46?d=identicon&s=25 Elton Santos (Guest)
on 2016-04-19 20:55
(Received via mailing list)
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 =(


=/
4c6bde00168d595053c09aac7e487f8e?d=identicon&s=25 Colin Law (Guest)
on 2016-04-19 21:26
(Received via mailing list)
On 19 April 2016 at 19:53, Elton Santos <elton.melo.santos@gmail.com>
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
2e903d3512efa714af85ed919b2ece46?d=identicon&s=25 Elton Santos (Guest)
on 2016-04-19 21:57
(Received via mailing list)
params[:authorization][:value_solve] = contract numbers checked
4c6bde00168d595053c09aac7e487f8e?d=identicon&s=25 Colin Law (Guest)
on 2016-04-19 22:09
(Received via mailing list)
On 19 April 2016 at 20:56, Elton Santos <elton.melo.santos@gmail.com>
wrote:
> params[:authorization][:value_solve] = contract numbers checked

No, I meant exactly what data is in it.

Colin
2e903d3512efa714af85ed919b2ece46?d=identicon&s=25 Elton Santos (Guest)
on 2016-04-19 22:32
(Received via mailing list)
This:

11
33

My contract numbers checked
2e903d3512efa714af85ed919b2ece46?d=identicon&s=25 Elton Santos (Guest)
on 2016-04-19 22:33
(Received via mailing list)
My contract examples: 11, 22, 33 and 44, respectivaly authorization id
1,
2, 3 and 4
4c6bde00168d595053c09aac7e487f8e?d=identicon&s=25 Colin Law (Guest)
on 2016-04-19 22:38
(Received via mailing list)
On 19 April 2016 at 21:30, Elton Santos <elton.melo.santos@gmail.com>
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
2e903d3512efa714af85ed919b2ece46?d=identicon&s=25 Elton Santos (Guest)
on 2016-04-19 23:22
(Received via mailing list)
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/Vxa...
But now, save, but is wrong, how show the image too.

<https://lh3.googleusercontent.com/-fgk8OIU6Has/Vxa...

T__T
2e903d3512efa714af85ed919b2ece46?d=identicon&s=25 Elton Santos (Guest)
on 2016-04-20 03:55
(Received via mailing list)
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
4c6bde00168d595053c09aac7e487f8e?d=identicon&s=25 Colin Law (Guest)
on 2016-04-20 09:25
(Received via mailing list)
On 20 April 2016 at 02:54, Elton Santos <elton.melo.santos@gmail.com>
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
2e903d3512efa714af85ed919b2ece46?d=identicon&s=25 Elton Santos (Guest)
on 2016-04-20 14:11
(Received via mailing list)
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"]}
2e903d3512efa714af85ed919b2ece46?d=identicon&s=25 Elton Santos (Guest)
on 2016-04-20 14:47
(Received via mailing list)
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 :D

:DDD
4c6bde00168d595053c09aac7e487f8e?d=identicon&s=25 Colin Law (Guest)
on 2016-04-20 15:15
(Received via mailing list)
On 20 April 2016 at 13:46, Elton Santos <elton.melo.santos@gmail.com>
wrote:
>>       end
Why are you using update_all?  You realise it doesn't run ActiveRecord
callbacks or validations.

Colin
2e903d3512efa714af85ed919b2ece46?d=identicon&s=25 Elton Santos (Guest)
on 2016-04-20 15:27
(Received via mailing list)
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 =
(
4c6bde00168d595053c09aac7e487f8e?d=identicon&s=25 Colin Law (Guest)
on 2016-04-20 17:22
(Received via mailing list)
On 20 April 2016 at 14:26, Elton Santos <elton.melo.santos@gmail.com>
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
2e903d3512efa714af85ed919b2ece46?d=identicon&s=25 Elton Santos (Guest)
on 2016-04-20 19:44
(Received via mailing list)
How I solve this? update instead of update_all ?

Em segunda-feira, 18 de abril de 2016 15:21:46 UTC-3, Elton Santos
escreveu:
4c6bde00168d595053c09aac7e487f8e?d=identicon&s=25 Colin Law (Guest)
on 2016-04-20 23:14
(Received via mailing list)
On 20 April 2016 at 18:42, Elton Santos <elton.melo.santos@gmail.com>
wrote:
> How I solve this? update instead of update_all ?

How to solve what?

Colin
2e903d3512efa714af85ed919b2ece46?d=identicon&s=25 Elton Santos (Guest)
on 2016-04-21 02:53
(Received via mailing list)
update instead of update_all, is it?


Em segunda-feira, 18 de abril de 2016 15:21:46 UTC-3, Elton Santos
escreveu:
This topic is locked and can not be replied to.