Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 如何在ActiveAdmin中筛选空值?_Ruby On Rails 3_Activeadmin_Formtastic_Meta Search - Fatal编程技术网

Ruby on rails 3 如何在ActiveAdmin中筛选空值?

Ruby on rails 3 如何在ActiveAdmin中筛选空值?,ruby-on-rails-3,activeadmin,formtastic,meta-search,Ruby On Rails 3,Activeadmin,Formtastic,Meta Search,我有一个名为map_id的整型列的表,如果该列为NULL或不为NULL,我想添加一个activeadmin筛选器来进行筛选 如何实现这一点 我尝试了以下过滤器 filter :map_id, :label => 'Assigned', :as => :select, :collection => {:true => nil, :false => ''} 但是,我收到以下错误消息: 未定义的方法“map\u eq”没有找到一个好的解决方案,但这里有一个方法。 Act

我有一个名为map_id的整型列的表,如果该列为NULL或不为NULL,我想添加一个activeadmin筛选器来进行筛选

如何实现这一点

我尝试了以下过滤器

filter :map_id, :label => 'Assigned', :as => :select, :collection => {:true => nil, :false => ''}
但是,我收到以下错误消息:


未定义的

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

在您的模型中:

  scope :field_blank, -> { where "field is null" }
  scope :field_not_blank, -> { where "field is not null" } 
对于:as=>:选择过滤器,acitve_admin使用_eq,这里是

在您的管理注册表块中:

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.

希望有此帮助。

在最近的rails版本中,搜索方法似乎不起作用,下面是另一个解决方案:

为模型添加范围:

  scope :field_blank, -> { where "field is null" }
  scope :field_not_blank, -> { where "field is not null" } 
添加到/app/admin/[您的型号]

   scope :field_blank
   scope :field_not_blank

您将看到这些作用域的按钮出现在顶部的model name下,而不是filter部分

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

关于管理 为了保持一致性,我从@Gret answer获取了相同的代码,只是更改了过滤器名称

在你的模型上 如果id为“none”,这将触发对nil的搜索,active record将返回所有nil id条目


帮助很大。

如果有人在这个线程上发生的太晚,现在有一种简单的方法可以在active admin中筛选空值或非空值:

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

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

对于可搜索作用域:

在ActiveAdmin资源定义中:

在您的模型上:

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

我做了一些编辑,如果你有权限,请快速查看并批准。我认为这是一个相当常见的问题,它的存在与工作信息。谢谢你的提问。我做了一些编辑,如果你有权限,请快速查看并批准。我认为这是一个相当常见的问题,它的存在与工作信息。感谢您最初的指导,您几乎完成了整个解决方案!谢谢另一个示例:嵌套在ActiveAdmin.register MyModel中,添加过滤器:available,:as=>:select,:label=>Availability,:collection=>{:available=>true,:Assigned=>false}。然后,在MyModel中实现范围:search\u method:available\u eq;作用域:available_eq,->{whereuser_id为{:除非ActiveRecord::ConnectionAdapters::Column.value_to_boolean_}null}谢谢,@gret-如果没有您的回答,将无法理解如何使我的示例正常工作请注意,这可能会导致SQL错误,具体取决于数据类型:它最适用于字符串值,并且在与日期时间一起使用时会中断。对于试图通过链接的查询字符串应用筛选器的任何人,您可以执行admin\u resources\u path'q[属性\u blank]'=>trueFor integer字段使用_null或_not_null代替:过滤器:属性_not_null,as::布尔过滤器:属性_null,as::boolean@Samuel-innovega answer也适用于日期时间/时间戳-对于日期/日期时间字段,使用_null或_not_null过滤器:datetime_attr_null,as::Boolean谢谢,这救了我的命。请注意,ActiveAdmin的默认设置不提供清除作用域的方法,因此如果要重置作用域,应手动定义并添加“一切”作用域。
filter :attribute_present, :as => :boolean 
filter :attribute_blank,   :as => :boolean  
filter :map_id, :label => 'Assigned', as: :select, :collection => [['Is Null', 'none'], ['Not null', 'present']]
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