开发者

How to have searchlogic initialize with no records

开发者 https://www.devze.com 2023-01-22 16:58 出处:网络
I\'m using Searchlogic in a model with tens of thousands of records and don\'t want to initially display them all the first time the search page loads. How do I get an empty search object from search开

I'm using Searchlogic in a model with tens of thousands of records and don't want to initially display them all the first time the search page loads. How do I get an empty search object from search开发者_运维知识库logic if there are no :search params?

  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


Change your logic to this:

def search
    @products = []
    @search = params[:search] && !params[:search].blank? ?
        Product.searchlogic(params[:search]) : nil
    @products = @search.all unless @search.nil?
end

Granted you could keep your if statement like so:

def search
    @products = []
    @search = nil
    if params[:search] && !params[:search].blank?
        Product.searchlogic(params[:search])
    end
    @products = @search.all unless @search.nil?
end
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号