【问题标题】:IF conditions in rails is not executingRails 中的 IF 条件未执行
【发布时间】:2013-04-20 07:49:24
【问题描述】:

我正在尝试根据用户输入的文本进行 solr 搜索。首先我要检查输入的文本是商店还是品牌或类别。检查该条件后,我想使用 solr 搜索显示 MainNavigationUrls 表中的相应 riesults。这是控制器动作。如果我进入任何商店并且可以获得正确的结果,它工作正常。但如果商店搜索结果为零,它就不会进入品牌循环。请解释为什么它没有执行第二种情况。

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

【问题讨论】:

    标签: ruby-on-rails search solr


    【解决方案1】:

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

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

    我认为您正在检查 @storesearch 是否包含某些元素?

    【讨论】:

    • 我得到了答案...我刚刚将 if(@storesearch.nil?) 替换为 if (@storesearch.results.empty?)... 感谢您的支持... . :)
    猜你喜欢
    • 2021-12-21
    • 1970-01-01
    • 2011-05-19
    • 1970-01-01
    • 1970-01-01
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多