1

Is there a way I can test for both these params to be true? Some sort of wildcard value for the first key?

params[:book][:return_to]
params[:work][:return_to]

At the moment I'm having to do:

if params[:book] and params[:book][:return_to]
   # blah
elsif params[:work] and params[:work][:return_to]
   # blah
snowangel
  • 3,452
  • 3
  • 29
  • 72
  • possible duplicate of [Is there a clean way to avoid calling a method on nil in a nested params hash?](http://stackoverflow.com/questions/5429790/is-there-a-clean-way-to-avoid-calling-a-method-on-nil-in-a-nested-params-hash) – Gene Feb 16 '15 at 22:08
  • Not quite a duplicate, I think -- it's not that I want to check params[:book] && params[:return_to] like in the other question -- I want to check params[:anything_at_all][:return_to]. – snowangel Feb 16 '15 at 22:28
  • Possible duplicate of [How to avoid NoMethodError for missing elements in nested hashes, without repeated nil checks?](http://stackoverflow.com/questions/4371716/how-to-avoid-nomethoderror-for-missing-elements-in-nested-hashes-without-repeat) – user513951 Jan 06 '16 at 05:25

1 Answers1

0

Hashie is the solution I've used.

https://github.com/intridea/hashie

From their readme:

user = {
  name: { first: 'Bob', last: 'Boberts' },
  groups: [
    { name: 'Rubyists' },
    { name: 'Open source enthusiasts' }
  ]
}

user.extend Hashie::Extensions::DeepFind

user.deep_find(:name)   #=> { first: 'Bob', last: 'Boberts' }
snowangel
  • 3,452
  • 3
  • 29
  • 72