Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

predicate present or true with null checkbox #434

Closed
kaluznyo opened this issue Sep 24, 2014 · 5 comments
Closed

predicate present or true with null checkbox #434

kaluznyo opened this issue Sep 24, 2014 · 5 comments

Comments

@kaluznyo
Copy link

Hi,

Before 1.4, i use check box with field_present or field_true. If the check box is not checked, no condition is add to request.

But since 1.4 if I use this, I get 'WHERE field != 1' when check box is not checked.

Seem to be related to this features #335.

What is the 'new' way to make this work again. No check -> ignore predicat.

@jonatack
Copy link
Contributor

Before looking into this, here is a copy-paste of my reply to a similar issue recently. This is still working fine in Ransack 1.4 for me.


For handling boolean results in searches, I've found I usually need three states: true, false, and all (default).

In that case, assuming your checkbox field has a checked value of 1 and an unchecked value of 0, the following in a search_form_for works, using a not_eq predicate:

  <%= f.select :field_not_eq, [['True', 0], ['False', 1], ['All', 2]] %>

or

  <%= f.select :field_not_eq, [['Yes', 0], ['No', 1], ['All', 2]] %>

@WayTang
Copy link

WayTang commented Oct 10, 2014

@Eric-Guo

@jonatack
Copy link
Contributor

jonatack commented Nov 5, 2014

If your unchecked value is '0', have you tried with field_not_false?

That should give you WHERE field != '0'.

@jonatack jonatack closed this as completed Nov 5, 2014
@aselder
Copy link

aselder commented Apr 18, 2016

@kaluznyo Don't know if you've seen this or not: http://stackoverflow.com/questions/27890997/ransack-return-records-with-boolean-attribute-both-true-and-false-with-checkbox

TL;DR include the include_hidden: false option

@aldosolis
Copy link

aldosolis commented Sep 20, 2016

@kaluznyo I did the following:

f.check_box :field_true, {}, 1, nil

nil is the value when is not checked. Did you find a simpler way?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants