简体   繁体   English

使用ActiveAdmin按序列化属性过滤

[英]Filter by serialized attribute using ActiveAdmin

I'm using ActiveAdmin and I need to build a custom filter, a combobox with abilities to filter users index list. 我正在使用ActiveAdmin,并且需要构建一个自定义过滤器,即具有过滤用户索引列表功能的组合框。 For example: 例如:

<select>
   <option value="designer">Diseñador</option>
   <option value="developer">Desarrollador</option>
   <option value="manager">Jefe de Proyecto</option>
</select>

I have users... 我有用户...

#<User id: 1, email: "un@tipo.com", abilities: {"designer"=>"Some comment", "developer"=>"Other comment", "manager"=>"Another comment"}>
#<User id: 2, email: "otro@tipo.com", abilities: {"designer"=>"blah blah"}>

As you can see, abilities attribute is defined on User model as: 如您所见, abilities属性用户模型上定义为:

serialize :abilities, Hash

So, when in the combobox I select designer I want to see two users on the list. 因此,在组合框中选择设计器时,我想在列表中看到两个用户 If instead I choose manager I want to see one user . 相反,如果我选择经理,我想看一个用户

I know that ActiveAdmin uses ransack to build filters but I don't know how to use this to work with serialized attributes. 我知道ActiveAdmin使用洗劫建立过滤器,但我不知道如何使用这个与序列化属性的工作。

I tried to use ransacker with the following code: 我尝试将ransacker与以下代码结合使用:

ActiveAdmin : ActiveAdmin

ActiveAdmin.register User do  
  filter :by_ability, as: :string, collection: ['designer', 'other']

Model : 型号

class User < ActiveRecord::Base
  scope :by_ability_eq, lambda {|v| User.where(["users.abilities LIKE '%?%'", v]) }
  ransacker :by_ability_eq

but I get this error : 但我得到这个错误

undefined method `by_ability_eq' for Ransack::Search<class: User, base: Grouping <combinator: and>>:Ransack::Search

I think this should work 我认为这应该工作

ActiveAdmin.register User do  
  filter :abilities_contains, as: :select, collection: ['designer', 'other']
  #....
end

In this case you don't need scope in model 在这种情况下,您不需要模型中的范围

UPD UPD

IF you need only keys to search, and your serializator uses YAML, you can try to customize it with something like 如果您只需要搜索键,并且您的serializator使用YAML,则可以尝试使用以下方式自定义它:

collection:  [["designer", "designer:"], ["other","other:"]]

Than it will use "designer:" and "other:" to search with LIKE 然后,它将使用“ designer:”和“ other:”来搜索LIKE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM