0

如何使用 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
}
4

1 回答 1

2
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
于 2015-08-05T18:28:21.237 回答