Ruby on Rails条件是哈希

时间:2013-08-19 13:56:54

标签: ruby-on-rails ruby hash conditional-statements

任何人都可以用一些代码帮助我:),我有一个沉闷的哈希

selector = {
 :fields => ['Id'],
 :predicates => [
   {:field => 'grpId', :operator => 'EQUALS', :values => [params_id]},
   {:field => 'CrType', :operator => 'EQUALS', :values => ['KEYWORD']}
 ]
}

我想为这一行创建一个条件:

{:field => 'grpId', :operator => 'EQUALS', :values => [params_id]},
仅当params_id不为空时才执行

谢谢

1 个答案:

答案 0 :(得分:0)

selector = {
 :fields => ['Id'],
 :predicates => [
   params_id ? {:field => 'grpId', :operator => 'EQUALS', :values => [params_id]} : nil,
   {:field => 'CrType', :operator => 'EQUALS', :values => ['KEYWORD']}
 ].compact
}