0

I would like to know the better way to extract hash value from params. The params looks like below (remember my params name is params[:ad_template]):

{"name"=>"name", 
 "description"=>"description", 
 "tag_list"=>"Software Development", 
 "objective"=>"VIDEO_VIEW", 
 "ad_creative_templates_attributes"=>{
   "0"=>{"creative_template_id"=>"2430", "_destroy"=>"false"}
 }, 
 "ad_target_templates_attributes"=>{
   "0"=>{"target_template_id"=>"1526", "_destroy"=>"false"}
 }
}

Now i would like to get the value of "creative_template_id" from this params.

I am being able to get this value by executing below code:

params[:ad_template][:ad_creative_templates_attributes].first.second[:creative_template_id]

Which gives me the value 2430 which is correct but i don't like this approach. SO i would like to know the better solution to extract that value from the params.

Thanks.

Uri Agassi
  • 36,848
  • 14
  • 76
  • 93
monsur
  • 601
  • 6
  • 18
  • You can search for specific key's value in deep hash, something like there: http://stackoverflow.com/questions/15031412/search-for-key-in-a-nested-hash-in-rails – Yevgeniy Anfilofyev Nov 19 '14 at 11:22

1 Answers1

2

How about:

params[:ad_template][:ad_creative_templates_attributes]['0'][:creative_template_id]
Uri Agassi
  • 36,848
  • 14
  • 76
  • 93