Searchkick在哪里哈希可选参数

时间:2015-08-05 18:14:27

标签: ruby-on-rails ruby searchkick

如何使用Ruby和Searchkick gem编写此代码。

https://github.com/ankane/searchkick#queries

# Works
@espacos = User.search where: {
  gender: params[:gender], # options: :male and :female
  other_param: param,
  other_param: param
}

# How do search all if the parameter is not informed?
@espacos = User.search where: {
  if not nil params[:gender]
    gender: params[:gender],
  end
  other_param: param,
  other_param: param
}

1 个答案:

答案 0 :(得分:1)

options = {
  other_param: other_param
  yet_another: yet_another
}

options[:gender] = params[:gender] if params[:gender].present?

编辑以满足以下要求

[:gender, :sex, :colour].each do |s|
  options[:s] = params[:s] if params[:s].present?
end