如何在ActiveAdmin中过滤IS NULL?

时间:2022-01-27 15:57:25

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.

我有一个带有名为“map_id”的整数列的表,我想添加一个activeadmin过滤器来过滤该列是否为NULL或IS NOT NULL。

How could this be implemented ?

怎么能实现呢?

I tried the following filter

我尝试了以下过滤器

filter :map_id, :label => 'Assigned', :as => :select, :collection => {:true => nil, :false => ''}

But, I get the following error message :

但是,我收到以下错误消息:

undefined method `map_eq' for #

用于#的未定义方法`map_eq'

5 个解决方案

#1


12  

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

没有找到一个好的解决方案,但这是一个如何。 Active_admin的过滤器由meta_search完成,您可以覆盖模型中meta_search自动生成的函数以获得您想要的行为,最好的方法是使用范围,因为您需要返回一个关系以便与其他查询链接/ scopes,如此处所述

in your model:

在你的模型中:

for :as=>:select filters, acitve_admin use the _eq wheres, here is the source code

for:as =>:选择过滤器,acitve_admin使用_eq wheres,这里是源代码

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

in your ative_admin register block:

在你的ative_admin寄存器块中:

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.

Hope this help.

希望这有帮助。

#2


18  

If anyone is happening on this thread belatedly, there is now an easy way to filter for null or non null in active admin :

如果有人在这个线程上发生了迟来的事情,现在有一种简单的方法可以在活动管理中过滤null或非null:

filter :attribute_present, :as => :boolean 
filter :attribute_blank,   :as => :boolean  

It is no longer necessary to add a custom method to the scope to accomplish this.

不再需要向作用域添加自定义方法来完成此操作。

#3


5  

seems search_method doesn't work in recent rails version, here is another solution:

似乎search_method在最近的rails版本中不起作用,这是另一个解决方案:

add scope to your model:

为您的模型添加范围:

  scope :field_blank, -> { where "field is null" }
  scope :field_not_blank, -> { where "field is not null" } 

add to /app/admin/[YOUR MODEL]

添加到/ app / admin / [您的型号]

   scope :field_blank
   scope :field_not_blank

you will see buttons for these scopes appear (in top section, under model name, not in filter section)

您将看到这些范围的按钮出现(在顶部,模型名称下,而不是在过滤器部分)

#4


1  

The new version of ActiveAdmin uses Ransacker. I manage to got it working this way:

新版本的ActiveAdmin使用Ransacker。我设法以这种方式工作:

On the admin

filter :non_nil_map_id, :label => 'Assigned', :as => :select, :collection => [['none', 'none'], ['one', 1],['tow', 2]]

For consistency, I took the same code from @Gret answer just changing the filter name

为了保持一致性,我从@Gret中获取了相同的代码,只需更改过滤器名称即可

On your model

ransacker :not_nil_map_id, :formatter => proc {|id|  map_id != 'none' ? id : 'none' } do |parent|
    parent.table[:id]
end

This should trigger a search against nil in case the id is 'none', and active record will return all the nil id entries.

如果id为'none',则应触发对nil的搜索,并且active record将返回所有nil id条目。

This thread helped a lot.

这个帖子帮了很多忙。

#5


-1  

With ransackable scopes:

有可复制的范围:

On the ActiveAdmin resource definition:

在ActiveAdmin资源定义上:

filter :map_id, :label => 'Assigned', as: :select, :collection => [['Is Null', 'none'], ['Not null', 'present']]

On your model:

在你的模型上:

scope :by_map_id, ->(id) { (id == 'none' ? where(map_id: nil) : where('map_id IS NOT NULL')) }

def self.ransackable_scopes(_auth_object = nil)
  %i[by_map_id]
end

#1


12  

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

没有找到一个好的解决方案,但这是一个如何。 Active_admin的过滤器由meta_search完成,您可以覆盖模型中meta_search自动生成的函数以获得您想要的行为,最好的方法是使用范围,因为您需要返回一个关系以便与其他查询链接/ scopes,如此处所述

in your model:

在你的模型中:

for :as=>:select filters, acitve_admin use the _eq wheres, here is the source code

for:as =>:选择过滤器,acitve_admin使用_eq wheres,这里是源代码

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

in your ative_admin register block:

在你的ative_admin寄存器块中:

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.

Hope this help.

希望这有帮助。

#2


18  

If anyone is happening on this thread belatedly, there is now an easy way to filter for null or non null in active admin :

如果有人在这个线程上发生了迟来的事情,现在有一种简单的方法可以在活动管理中过滤null或非null:

filter :attribute_present, :as => :boolean 
filter :attribute_blank,   :as => :boolean  

It is no longer necessary to add a custom method to the scope to accomplish this.

不再需要向作用域添加自定义方法来完成此操作。

#3


5  

seems search_method doesn't work in recent rails version, here is another solution:

似乎search_method在最近的rails版本中不起作用,这是另一个解决方案:

add scope to your model:

为您的模型添加范围:

  scope :field_blank, -> { where "field is null" }
  scope :field_not_blank, -> { where "field is not null" } 

add to /app/admin/[YOUR MODEL]

添加到/ app / admin / [您的型号]

   scope :field_blank
   scope :field_not_blank

you will see buttons for these scopes appear (in top section, under model name, not in filter section)

您将看到这些范围的按钮出现(在顶部,模型名称下,而不是在过滤器部分)

#4


1  

The new version of ActiveAdmin uses Ransacker. I manage to got it working this way:

新版本的ActiveAdmin使用Ransacker。我设法以这种方式工作:

On the admin

filter :non_nil_map_id, :label => 'Assigned', :as => :select, :collection => [['none', 'none'], ['one', 1],['tow', 2]]

For consistency, I took the same code from @Gret answer just changing the filter name

为了保持一致性,我从@Gret中获取了相同的代码,只需更改过滤器名称即可

On your model

ransacker :not_nil_map_id, :formatter => proc {|id|  map_id != 'none' ? id : 'none' } do |parent|
    parent.table[:id]
end

This should trigger a search against nil in case the id is 'none', and active record will return all the nil id entries.

如果id为'none',则应触发对nil的搜索,并且active record将返回所有nil id条目。

This thread helped a lot.

这个帖子帮了很多忙。

#5


-1  

With ransackable scopes:

有可复制的范围:

On the ActiveAdmin resource definition:

在ActiveAdmin资源定义上:

filter :map_id, :label => 'Assigned', as: :select, :collection => [['Is Null', 'none'], ['Not null', 'present']]

On your model:

在你的模型上:

scope :by_map_id, ->(id) { (id == 'none' ? where(map_id: nil) : where('map_id IS NOT NULL')) }

def self.ransackable_scopes(_auth_object = nil)
  %i[by_map_id]
end