我在拥有数万条记录的模型中使用Searchlogic,并且不希望在第一次加载搜索页面时首先显示它们。如果没有:search params,如何从searchlogic获取空搜索对象?
def search @products = [] if params[:search] && !params[:search].blank? @search = Product.searchlogic(params[:search]) else @search = Product.searchlogic(....What goes here to get an empty searchlogic object?...) end @products = @search.all end
答案 0 :(得分:1)
将您的逻辑更改为:
def search
@products = []
@search = params[:search] && !params[:search].blank? ?
Product.searchlogic(params[:search]) : nil
@products = @search.all unless @search.nil?
end
当然,你可以保留你的if语句:
def search
@products = []
@search = nil
if params[:search] && !params[:search].blank?
Product.searchlogic(params[:search])
end
@products = @search.all unless @search.nil?
end