eq_any for boolean attributes - ransack
Asked Answered
L

3

6

I have a boolean attribute (published) in my model book and I wish to filter all books using checkboxes on that value.

class Book < ActiveRecord::Base
  attr_accessible :published
end

That means I'd like something like eq_any but for true or false. Is there a way to do this using Ransack?

UPDATE

I'd like users to be able to select only published books, only unpublished books and any book. So a single checkbox won't do.

Ligure answered 5/7, 2013 at 13:49 Comment(0)
E
5

The choosen answer works fine, but, at least in 2016, something better can be written. Leveraging on the "eq" functionality allows to remove some configuration. The final result is:

<%= f.select :published_eq, [['Yes', true], ['No', false]], include_blank: 'All' %>
Ender answered 14/11, 2016 at 9:24 Comment(1)
please correct the syntax <%= f.select :published_eq, [['Yes', true], ['No', false]], include_blank: 'All' %>Mountfort
G
4

I have solved this by using a select list with three options: "All", "Yes" and "No"

= select :q, :published_true, [['Yes', 1], ['No', 0]], { include_blank: 'All', selected: params[:q] ? params[:q][:published_true] : '' }

The query string for published_true will look like this:

q[published_true]=1 1 will return published books

q[published_true]=0 0 will return unpublished books

q[published_true]= blank – will return both published and unpublished books

Gayegayel answered 26/11, 2014 at 18:40 Comment(0)
W
2

Renan, you can do this using the 'true' and 'false' predicates (other predicates are listed in the documentation link below).

In your form, your checkbox code would look something like this:

<%= f.checkbox :published_true %>
<%= f.checkbox :published_false %>

So, in your form, checking the first checkbox (published_true) would return all books where published is true, checking the second box (published_false) would return all books where published is false (unplublished), and submitting the form without checking either box would return all books.

More information can be found in the documentation here: https://github.com/ernie/ransack/wiki/Basic-Searching

Winslow answered 12/8, 2013 at 16:54 Comment(4)
But if I do as you say, when I submit the form with the checkbox unchecked it will only retrieve books where published is not true. I'd like to be able to have two checkboxes (one for published and the other for not published) and when I check both it will actually perform an OR and not an AND.Ligure
@Ligure Have you had a chance to actually try the code? I have a few similar searches on a project that I'm working on and when I submit the form w/o checking the checkbox, ransack ignores it and returns all results.Winslow
You are right, but now that I think of it (this is a fairly old question of mine, so I actually got around this problem already), I don't want only one checkbox. I want the user to be able to select only unpublished books too, which your solution would not do. I'll update the question to make it clearer.Ligure
I updated my answer. I've had my share of ransack headaches, hopefully this helps.Winslow

© 2022 - 2024 — McMap. All rights reserved.