2014-11-19 4 views
0

params에서 해시 값을 추출하는 더 좋은 방법을 알고 싶습니다. params 객체를 파라미터는 (기억 내 PARAMS 이름이있는 params이다 : ad_template]) : 다음과 같은params에서 해시 값 가져 오기

{"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"} 
} 
} 

지금 내가이 PARAMS에서 "creative_template_id"의 ​​값을 좀하고 싶습니다.

나는 코드 아래 실행하여이 값을 얻을 수있는 오전 : 나에게 올바른 값 2430을 제공하지만 난이 방법을 좋아하지 않는다

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

합니다. 그래서 params에서 그 값을 추출하는 더 나은 솔루션을 알고 싶습니다.

감사합니다.

+0

, 뭔가 : http://stackoverflow.com/questions/15031412/search-for-key-in-a- 중첩 된 해시 - 인 - 레일 –

답변

2

방법에 대해 :

params[:ad_template][:ad_creative_templates_attributes]['0'][:creative_template_id] 
이 같은 당신은 깊은 해시의 특정 키 값을 검색 할 수 있습니다
+0

귀하의 솔루션에 대해 @Uri Agassi에게 감사드립니다. 최선의 해결책인지 알고 싶습니다. – monsur

관련 문제