导轨在一个组合框中meta_search更大和更少的条件



我有一个带有checked_by字段的模型Phone;如果此字段等于1,那么我们知道此手机未选中,否则(>1) - 选中。在管理方面,我可以查看Phones列表,我需要使用 meta_search 创建一个过滤器来查看:

  • 所有电话
  • 检查
  • 猖獗

我可以在meta_search中看到checked_by_greater_thanchecked_by_less_than方法,但是如何将这些方法组合在一个选择框中?

感谢任何建议

带有范围和虚构字段。

适用范围:

class Phone < ActiveRecord::Base
  scope :checked, lambda { |value| 
      !value.zero? ? checked_by_greater_than(1) : where(:checked_by => 1)
  }
end

然后添加一个包含三个值的选择框,将[nil, 0, 1]作为值返回,并在控制器中使用该参数应用新作用域。

class PhonesController < ApplicationController
  def index
    # ...
    @phones ||= Phone.scoped
    checked_select_value = params.delete("checked_select") # here use the name of your form field
    if checked_select_value.present?
      @phones = @phones.checked(checked_select_value.to_i)
    end
    # now apply the rest of your meta-search things to the @phones
    #
  end
end

最新更新