sbonkosky
sbonkosky

Reputation: 2557

Unable to add to params hash using merge

I can't seem to get new values added my my params hash. I'm trying to add this actor_id key to params but this isn't working:

params.merge(:actor_id => 2)

When I use logger.debug before and after the merge I don't see my actor_id key. How do I add to params?

Upvotes: 1

Views: 1256

Answers (2)

aarti
aarti

Reputation: 2865

This is because the method merge in ActiveSupport::HashWithIndifferentAccess does not modify the receiver but rather returns a new hash with indifferent access with the result of the merge.

As the comments have suggested use merge! or use update which is an alias.

ActiveController::Parameters inherits from ActiveSupport::HashWithIndifferentAccess

    # This method has the same semantics of +update+, except it does not
    # modify the receiver but rather returns a new hash with indifferent
    # access with the result of the merge.
    def merge(hash, &block)
      self.dup.update(hash, &block)
    end

Upvotes: 1

Rashmi Nair
Rashmi Nair

Reputation: 339

try this

params.merge!(actor_id: 2)

it will modify the params itself as we are using the !

Upvotes: 3

Related Questions