[英]Filter by serialized attribute using ActiveAdmin
我正在使用ActiveAdmin,並且需要構建一個自定義過濾器,即具有過濾用戶索引列表功能的組合框。 例如:
<select>
<option value="designer">Diseñador</option>
<option value="developer">Desarrollador</option>
<option value="manager">Jefe de Proyecto</option>
</select>
我有用戶...
#<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"}>
如您所見, abilities屬性在用戶模型上定義為:
serialize :abilities, Hash
因此,在組合框中選擇設計器時,我想在列表中看到兩個用戶 。 相反,如果我選擇經理,我想看一個用戶 。
我知道ActiveAdmin使用洗劫建立過濾器,但我不知道如何使用這個與序列化屬性的工作。
我嘗試將ransacker與以下代碼結合使用:
ActiveAdmin :
ActiveAdmin.register User do
filter :by_ability, as: :string, collection: ['designer', 'other']
型號 :
class User < ActiveRecord::Base
scope :by_ability_eq, lambda {|v| User.where(["users.abilities LIKE '%?%'", v]) }
ransacker :by_ability_eq
但我得到這個錯誤 :
undefined method `by_ability_eq' for Ransack::Search<class: User, base: Grouping <combinator: and>>:Ransack::Search
我認為這應該工作
ActiveAdmin.register User do
filter :abilities_contains, as: :select, collection: ['designer', 'other']
#....
end
在這種情況下,您不需要模型中的范圍
UPD
如果您只需要搜索鍵,並且您的serializator使用YAML,則可以嘗試使用以下方式自定義它:
collection: [["designer", "designer:"], ["other","other:"]]
然后,它將使用“ designer:”和“ other:”來搜索LIKE
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.