Gabe Kopley
2014-Nov-10 19:28 UTC
Add ActionController::Parameters#dig to grab values out of nested params, failing gracefully
Hi folks,
For better or worse, this is a pattern you see pretty often in real-world
Rails code [0]:
params[:order] && params[:order][:shipping_info] &&
params[:order][:shipping_info][:country]
or
params[:order][:shipping_info][:country] rescue nil
or
params.fetch(:order, {}).fetch(:shipping_info, {}).fetch(:country, nil)
What if we had a method to accomplish this more concisely?
Eg.
params.traverse_nested_params_and_return_nil_if_a_key_isnt_found(:order,
:shipping_info, :country)
Or to take a nice method name suggestion [1]:
params.dig(:order, :shipping_info, :country)
Another example solution is https://github.com/intridea/hashie#deepfetch
(Although
I don't like "fetch" in this method name since it doesn't and
can't take a
default value as an argument like Hash#fetch does)
Would a method like this possibly belong in ActionController::Parameters?
Or maybe even in Active Support extensions on Hash (gasp!)? Or is it just
an all-around terrible idea?
Thanks,
Gabe
[0] Sources:
https://groups.google.com/d/msg/rubyonrails-core/bOkvcvS3t_A/QXLEXwt9ivAJ
https://stackoverflow.com/questions/1820451/ruby-style-how-to-check-whether-a-nested-hash-element-exists
https://stackoverflow.com/questions/19115838/how-do-i-use-the-fetch-method-for-nested-hash
https://stackoverflow.com/questions/10130726/ruby-access-multidimensional-hash-and-avoid-access-nil-object
[1] http://stackoverflow.com/a/1820492/283398
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to rubyonrails-core+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-core@googlegroups.com.
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.