Problem with query and update table on db

39 views
Skip to first unread message

Elton Santos

unread,
Apr 18, 2016, 2:21:46 PM4/18/16
to Ruby on Rails: Talk
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 =(

Colin Law

unread,
Apr 18, 2016, 3:58:55 PM4/18/16
to Ruby on Rails: Talk
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

>
> --
> 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-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/18f63da3-c79e-423e-9f3e-cd81ae33f022%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Elton Santos

unread,
Apr 18, 2016, 10:16:21 PM4/18/16
to Ruby on Rails: Talk
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"}


Colin Law

unread,
Apr 19, 2016, 2:04:47 AM4/19/16
to Ruby on Rails: Talk

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-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.

> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/b1b62337-bda4-4362-b359-de4d56e50c0c%40googlegroups.com.

Elton Santos

unread,
Apr 19, 2016, 9:02:21 AM4/19/16
to Ruby on Rails: Talk
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'

Colin Law

unread,
Apr 19, 2016, 10:12:44 AM4/19/16
to Ruby on Rails: Talk
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

Elton Santos

unread,
Apr 19, 2016, 2:53:40 PM4/19/16
to Ruby on Rails: Talk
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 =(


=/




Colin Law

unread,
Apr 19, 2016, 3:25:56 PM4/19/16
to Ruby on Rails: Talk
On 19 April 2016 at 19:53, Elton Santos <elton.me...@gmail.com> wrote:
> 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')
>

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

Colin

Elton Santos

unread,
Apr 19, 2016, 3:56:15 PM4/19/16
to Ruby on Rails: Talk
params[:authorization][:value_solve] = contract numbers checked

Colin Law

unread,
Apr 19, 2016, 4:08:27 PM4/19/16
to Ruby on Rails: Talk
On 19 April 2016 at 20:56, Elton Santos <elton.me...@gmail.com> wrote:
> params[:authorization][:value_solve] = contract numbers checked

No, I meant exactly what data is in it.

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-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/c5541121-0b8d-400e-83ec-5cb525cda588%40googlegroups.com.

Elton Santos

unread,
Apr 19, 2016, 4:30:47 PM4/19/16
to Ruby on Rails: Talk
This:

11
33

My contract numbers checked

Elton Santos

unread,
Apr 19, 2016, 4:32:30 PM4/19/16
to Ruby on Rails: Talk
My contract examples: 11, 22, 33 and 44, respectivaly authorization id 1, 2, 3 and 4

Colin Law

unread,
Apr 19, 2016, 4:37:55 PM4/19/16
to Ruby on Rails: Talk
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

Elton Santos

unread,
Apr 19, 2016, 5:21:20 PM4/19/16
to Ruby on Rails: Talk
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

Elton Santos

unread,
Apr 19, 2016, 9:54:44 PM4/19/16
to Ruby on Rails: Talk
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 

Colin Law

unread,
Apr 20, 2016, 3:25:07 AM4/20/16
to Ruby on Rails: Talk
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

Elton Santos

unread,
Apr 20, 2016, 8:10:13 AM4/20/16
to Ruby on Rails: Talk
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"]}
-----
  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"]]

Elton Santos

unread,
Apr 20, 2016, 8:46:15 AM4/20/16
to Ruby on Rails: Talk
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 

Colin Law

unread,
Apr 20, 2016, 9:14:25 AM4/20/16
to Ruby on Rails: Talk
On 20 April 2016 at 13:46, Elton Santos <elton.me...@gmail.com> wrote:
> 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

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

Colin

Elton Santos

unread,
Apr 20, 2016, 9:26:15 AM4/20/16
to Ruby on Rails: Talk
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 = (

Colin Law

unread,
Apr 20, 2016, 11:21:12 AM4/20/16
to Ruby on Rails: Talk
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

Elton Santos

unread,
Apr 20, 2016, 1:42:55 PM4/20/16
to Ruby on Rails: Talk
How I solve this? update instead of update_all ?

Colin Law

unread,
Apr 20, 2016, 5:13:19 PM4/20/16
to Ruby on Rails: Talk
On 20 April 2016 at 18:42, Elton Santos <elton.me...@gmail.com> wrote:
> How I solve this? update instead of update_all ?

How to solve what?

Colin

Elton Santos

unread,
Apr 20, 2016, 8:52:16 PM4/20/16
to Ruby on Rails: Talk
update instead of update_all, is it?


Em segunda-feira, 18 de abril de 2016 15:21:46 UTC-3, Elton Santos escreveu:
Reply all
Reply to author
Forward
0 new messages