Question

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

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top