问题描述
我有一个有很多照片的User
模型.我希望在Active Admin中设置一个复选框过滤器,以过滤那些拥有照片的用户.基本上是存在照片关联的地方.
I have a User
model that has_many photos. I'm looking to set up a checkbox filter in Active Admin to filter those users who have photos. Basically where the photos association is present.
class User < ActiveRecord::Base
has_many :photos
end
有没有简单的方法可以做到这一点?我知道您可以按拥有特定照片等的用户进行过滤.但是我还没有看到可以按在线状态进行过滤的示例.
Is there an easy way to do this? I know you can filter by users who have a certain photo etc. but I haven't seen an example where you can filter by presence.
推荐答案
找到正确的Ransack搜索方法是很棘手的.用以下过滤器搜索photos.id IS NOT NULL
在哪里可以实现:
Finding the correct incantation of Ransack search methods is tricky. To search where the photos.id IS NOT NULL
can be accomplished with the following filter:
ActiveAdmin.register User do
# Filter users where photos.id is not null
filter :photos_id_not_null, label: "With Photos", as: :boolean
end
这篇关于活动管理员-按has_many关联的存在进行过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!