如何在ActiveAdmin中过滤IS NULL? [英] How to filter IS NULL in ActiveAdmin?
问题描述
我有一个表,该表带有一个名为 map_id的整数列,我想添加一个activeadmin过滤器以过滤此列是NULL还是IS NOT NULL。
I've a table with an integer column called "map_id", I want to add an activeadmin filter to filter if this column IS NULL or IS NOT NULL.
如何实现?
我尝试了以下过滤器
filter :map_id, :label => 'Assigned', :as => :select, :collection => {:true => nil, :false => ''}
但是,我收到以下错误消息:
But, I get the following error message :
未定义方法 map_eq,用于#
undefined method `map_eq' for #
推荐答案
找不到很好的解决方案,但这是一个方法。 Active_admin的
过滤器由meta_search完成,您可以覆盖模型中meta_search自动生成的函数以获得所需的行为,最好的方法是使用范围,因为您需要返回一个关系才能此处
Not found a good solution but here is a how. filters of Active_admin are accomplished by meta_search, you can override the functions automatically generated by meta_search in your model to get the behavior that you want, the best way is to use the scope since you need to return a relation in order to chain with other query/scopes, as stated here
在您的模型中:
用于:as =>:选择过滤器,acitve_admin使用_eq wheres,这里是源代码
for :as=>:select filters, acitve_admin use the _eq wheres, here is the source code
scope :map_eq,
lambda{ |id|
if(id !='none')
where( :map_id=> id)
else
where( :map_id=> nil)
end
}
#re-define the search method:
search_method :map_eq, :type => :integer
:
filter :map_id, :label => 'Assigned', :as => :select, :collection => [['none', 'none'], ['one', 1],['tow', 2]]
# not using :none=>nil because active_admin will igore your nil value so your self-defined scope will never get chained.
希望获得帮助。
这篇关于如何在ActiveAdmin中过滤IS NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!