tlehman
tlehman

Reputation: 5167

Hash#merge with default value

Currently, I have a method on ApplicationController I call pagination_params that uses strong_params to whitelist some keys

def pagination_params
  params.permit(:page, :per_page, :after_id)
end

I would like to override any values using .merge like so, but I want to use a default value, the ugly workaround I am using for this is:

pagination_params.merge(:per_page => pagination_params.fetch(:per_page, DEFAULT_PER_PAGE))

Is there a cleaner way to do this?

Upvotes: 1

Views: 428

Answers (2)

jasonleonhard
jasonleonhard

Reputation: 13907

I think reverse_merge only merges key/values that don’t already exist in the hash.

Upvotes: 1

Dangyi Liu
Dangyi Liu

Reputation: 329

DEFAULT_PARAMS_HASH.merge pagination_params

Also, use string as keys in DEFAULT_PARAMS_HASH rather than symbol.

Upvotes: 0

Related Questions