代码之家  ›  专栏  ›  技术社区  ›  Rubioli

Rails 4/Filterrific gem-布尔域问题

  •  0
  • Rubioli  · 技术社区  · 7 年前

    tested 这是一个 boolean

    checkbox 用户可以在其中基于 测试 .

    filterrific :default_filter_params => { :sorted_by => 'created_at_desc' },
                  :available_filters => %w[
                    sorted_by
                    search_query
                    with_created_at_gte
                    with_tested
                  ]
    scope :with_tested, lambda { |flag|
        return nil  if 0 == flag # checkbox unchecked
        where(tested: true)
    }
    
    /// Other scopes
    

    在我看来,我有:

    = f.check_box :with_tested
    

    在我的模型中,我也尝试过不同的方法,但没有运气:

    scope :with_tested, lambda { |value|
      where('posts.tested = ?', value)
    }
    
    // and 
    
    scope :with_tested, lambda { |query|
      return nil  if 0 == query # checkbox unchecked
      where('posts.tested == ?', query)
    }
    
    // and
    
    scope :with_tested, lambda { |flag|
        return nil  if 0 == flag # checkbox unchecked
        where(tested: [flag])
    }
    

    测试 ,我可以看到我的过滤器正在尝试过滤 (我看到过滤器旋转) ,但未正确筛选我的记录。

    我不确定我做错了什么。任何建议和帮助都将不胜感激!

    附言:我还没有补充 with_tested 在我的控制器里,因为我知道我不需要它


    版本:

    RubyonRails:4.2.4

    过滤系数:2.1.2

    1 回复  |  直到 7 年前
        1
  •  0
  •   Rubioli    7 年前

    问题是 where(tested: [flag]) ,因为还没有 设置 true false where 应该 怎么回事 value 那个案子

    所以呢 应更改为: where(tested: true) where(tested: false) .

    首先,您需要在 model :

    scope :with_tested, lambda { |flag|
          return nil  if 0 == flag # checkbox unchecked
          where(tested: true)
        }
    

    在你的 view

    = f.check_box :with_tested
    

    并添加 :with_tested 在你的控制器里 available_filters .

    推荐文章