开发者

How to filter a boolean column on ActiveAdmin?

开发者 https://www.devze.com 2023-04-09 20:25 出处:网络
This question is related to the ActiveAdmin gem. I\'m trying to filter a column that has a boolean type but with no success: filter :column_name开发者_JAVA技巧 and filter :column_name, :as => :bool

This question is related to the ActiveAdmin gem. I'm trying to filter a column that has a boolean type but with no success: filter :column_name开发者_JAVA技巧 and filter :column_name, :as => :boolean don't work.

Any idea?

Thanks!


filter :column_name, :as => :select will create a drop down with values "Any", "True", "False"


As of ActiveAdmin 0.6.2, using filter :column_name, as: :select now has the horrible side effect of doing a complete table scan. Plus, its options are now "Any", "true", "false".

For example, if I have a District model with the boolean column enabled then filter :enabled, as: :select generates the query SELECT DISTINCT "districts"."enabled" FROM "districts" ORDER BY enabled asc to obtain the 3 values. My districts table is quite large, so this is clearly not what I want.

OTOH, while I can now use filter :column_name, as: :boolean, it uses a checkbox that defaults to not being checked, which is again not what I want.

To restore the pre-0.6.2 behavior, I had to do this: filter :enabled, as: :select, collection: [["Yes", true], ["No", false]]. ActiveAdmin drops in the "Any" value for me.


Similarly, if you need to use checkboxes, do:

filter :column_name, as: :check_boxes

This will create checkboxes with the names Yes and No, whose values are true and false respectively.

If you want to customise the names, and maybe even the label? Then do:

filter :column_name, label: "Custom Column Name", as: :check_boxes, collection: [["Paid", true], ["Not paid", false]]

Though you can use checkboxes(it works!), I recommend using as::select as it 'remembers' the chosen value (on the UI) after page refresh. Checkboxes don't yet have this feature as of activeadmin version 1.2.1

0

精彩评论

暂无评论...
验证码 换一张
取 消