Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Active Admin - filter by presence of has_many association

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.

like image 865
johnnymire Avatar asked Jan 06 '15 11:01

johnnymire


1 Answers

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
like image 176
Charles Maresh Avatar answered Oct 03 '22 04:10

Charles Maresh