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