简体   繁体   中英

Rails meta_search greater and less conditions in one combobox

I have a model Phone with checked_by field; if this field is equal to 1, then we know this phone is unchecked , else(>1) - checked . On admin side I can review a list of Phones and I need to create a filter using meta_search to review:

  • All Phones
  • Checked
  • Unchecked

I can see checked_by_greater_than , or checked_by_less_than methods in meta_search, but how to combine those methods in a single select box?

Thanks in any advise

With a scope and a made-up field.

The scope:

class Phone < ActiveRecord::Base

  scope :checked, lambda { |value| 
      !value.zero? ? checked_by_greater_than(1) : where(:checked_by => 1)
  }

end

Then add a select-box with three values, returning [nil, 0, 1] as values, and in your controller use that parameter to apply the new scope.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM