Skip to content

Weird params behavior #1828

Closed
Closed

Description

@usern3

I was trying to modify the params I was receiving and I was struggling to convert the keys to symbols. I eventually had to settle on params.to_h.transform_keys(&:to_sym) to get it to work. Now, I could've sworn I've never had to do this in the past, so potentially I screwed something up, I just have no idea what. If it isn't an error on my end though, here's some of my output:

#567354) params.is_a? Hash
true
(ruby@puma: cluster worker 1: 567080 [backend]#567354) params.transform_keys(&:to_sym)
{"name"=>"Device 1",
 "device_id"=>"D1234",
 "scheduled_at"=>"2022-10-19 18:30:08 +0000",
 "location"=>"office1",
 "first_name"=>"dsf",
 "last_name"=>"zdsfgdf",
 "password"=>"dsgdf",
 "email"=>"dsfgsd@sdfs.com"}

(ruby@puma: cluster worker 1: 567080 [backend]#567354) params.transform_keys!(&:to_sym)
{"name"=>"Device 1",
 "device_id"=>"D1234",
 "scheduled_at"=>"2022-10-19 18:30:08 +0000",
 "location"=>"office1",
 "first_name"=>"dsf",
 "last_name"=>"zdsfgdf",
 "email"=>"dsfgsd@sdfs.com"}

(ruby@puma: cluster worker 1: 567080 [backend]#567354) params.symbolize_keys(&:to_sym)
{"name"=>"Device 1",
 "device_id"=>"D1234",
 "scheduled_at"=>"2022-10-19 18:30:08 +0000",
 "first_name"=>"dsf",
 "last_name"=>"zdsfgdf",
 "password"=>"dsgdf",
 "email"=>"dsfgsd@sdfs.com"}

(ruby@puma: cluster worker 0: 567080 [backend]#567813) params.to_h.transform_keys(&:to_sym)
{:name=>"Device 1",
 :device_id=>"D1234",
 :scheduled_at=>"2022-10-19 18:30:08 +0000",
 :location=>"office1",
 :first_name=>"dsf",
 :last_name=>"zdsfgdf",
 :password=>"dsgdf",
 :email=>"dsfgsd@sdfs.com"}

Activity

  1. dentarg commented on Oct 20, 2022

    @dentarg
    Member
  2. usern3 commented on Oct 21, 2022

    @usern3
    Author

    params is not a real hash, see https://github.com/sinatra/sinatra/blob/v3.0.2/lib/sinatra/indifferent_hash.rb

    some more info in #1592

    Okay I see now. I'm still confused as to why params.transform_keys(&:to_sym) doesn't work though since IndifferentHash seems to have transform_keys methods?

        def transform_keys(&block)
          dup.transform_keys!(&block)
        end
    
        def transform_keys!
          super
          super(&method(:convert_key))
        end
  3. dentarg commented on Oct 21, 2022

    @dentarg
    Member

    convert_key always converts keys to strings

  4. usern3 commented on Oct 21, 2022

    @usern3
    Author

    convert_key always converts keys to strings

    ah gotcha okay thanks for clearing that up.

  5. NobodysNightmare commented on Nov 22, 2022

    @NobodysNightmare

    Now, I could've sworn I've never had to do this in the past, so potentially I screwed something up

    Looking at the timing you brought this up, did your code include a select or reject call before using transform_keys in the past?

    I was wondering the same (some code broke after an upgrade) and it turns out that these methods did return regular hashes before the upgrade (and thus transform_keys worked) but return an indifferent hash after the upgrade. See #1711

  6. usern3 commented on Nov 22, 2022

    @usern3
    Author

    Looking at the timing you brought this up, did your code include a select or reject call before using transform_keys in the past?

    I don't recall using select or reject previously. So, if you used one of those before it returned a regular ruby hash?

  7. NobodysNightmare commented on Nov 22, 2022

    @NobodysNightmare
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions