Search code examples
ruby-on-railsransack

Ransack opposite predicate :present


Ransack has abilities finding User that has a first_name:

User.search(:first_name_present => "1")

and the opposite, finding User with no first_name or blank:

User.search(:first_name_blank => "1")

.

Unfortunately,

User.search(:first_name_present => "0") not equal with

User.search(:first_name_blank => "1")

.

Is there anyway to make

User.search(:first_name_present => "0") equal

User.search(:first_name_blank => "1"),

or

User.search(:first_name_present => "1") equal

User.search(:first_name_blank => "0")?

Thx


Solution

  • You could of course do something like this before calling search:

    params[:q][:first_name_present] = "1" if
      params[:q][:first_name_blank].nil? ||
      params[:q][:first_name_blank] == "0"
    

    I think it's good that these are not the same thing, otherwise, you couldn't have a checkbox to only find those users with empty first name – it would not allow a state listing all users. As it is now, unchecked checkboxes do not cause filtering.