2013-04-20 46 views
0

我正在尝试基于用户输入的文本进行solr搜索。我必须检查输入的文字是商店还是品牌或类别。检查完条件后,我想使用solr搜索显示MainNavigationUrls表中相应的riesults。这是控制器操作。如果我进入任何商店并且可以得到适当的结果,它工作正常。但是,如果商店搜索被发现,它不会进入品牌循环。请解释为什么它不执行第二种情况。如果rails中的条件未执行

def search_all 
    @storesearch=Site.search do 
     fulltext params[:text] 
     with(:multibrand,1) 
    end 

    if(@storesearch.nil?) 
     @brandsearch=Brand.search do 
     fulltext params[:text] 
     end 
     if(@brandssearch.nil?) 
     @itemcategorysearch=MainCategory.search do 
      fulltext params[:text] 
     end 
     @itemcategorysearch.results.each do |result| 
      @itemcategorysearch1=MainNavigationUrl.search do 
      with :main_categoty_id, result.id 
      with :department_id, params[:deptid].to_i 
      paginate :page=>params[:page], :per_page=>45 
      end 
     end 
     else 
     @brandsearch.results.each do |result| 
      @brandsearch1=MainNavigationUrl.search do 
      with :brand_id, result.id 
      with :department_id, params[:deptid].to_i 
      paginate :page=>params[:page], :per_page=>45 
      end 
     end 

    else 
     @storesearch.results.each do |result| 
     @storesearch1=MainNavigationUrl.search do 
      with :site_id, result.id 
      with :department_id, params[:deptid].to_i 
      paginate :page=>params[:page], :per_page=>45 
     end 
     end 
    end 
    end 

回答

0

而不是.nil? ,也许你可以使用.blank?更好?

@array = [] 
@array.nil? -false 
@array.blank? -true 

我想你正在检查@storesearch是否包含一些元素?

+0

我得到了答案...我只是用if(@ storesearch.results.empty?)替换(@ storesearch.nil?)......感谢您的支持.... :) – user2218532 2013-04-20 10:21:17