Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails Active Admin - Duplicate Records showing for HABTM

I am designing a basic file manager (the Asset model) in the Active Admin gem. Each Asset HABTM Groups, and vice-versa.

In my active_admin Asset resource I have a filter where I want to be able to select multiple groups to filter by, so I added:

filter :groups_id, :as => :check_boxes, :collection => proc {Group.all}

All of the groups show up as checkboxes as expected. However, if I have asset_1, asset_2 and I have group_1 assigned to asset_1 and asset_2, and group_2 to asset_2, when I filter by both roles, asset_2 lists itself twice.

How can I restrict the filter to use only "distinct" or "unique" assets to be returned?

I also have another problem, which is that the filters are not working at all in any of my scopes.

like image 864
professormeowingtons Avatar asked Dec 22 '22 02:12

professormeowingtons


1 Answers

A quick update on Will's answer. I'm running Rails 5.0 and ActiveAdmin 1.0, and clean_search_params returned an error. But this worked instead:

def apply_filtering(chain)
  super
  @search.result(distinct: true)
end

Thanks!

like image 82
Alex F. Avatar answered Dec 23 '22 17:12

Alex F.