0
votes

So I'm in Active Admin and in the following model a Degree belongs to a user and a User has many degrees

For a User we have a site_id that identifies where the User works in a Site table

So in my filter in Active Admin's degree model, I can't seem to pull a list of sites that someone can filter from in the Active Admin UI.

Here are my filters so far: major institution completion_date

These are straight from the Degree tables

:user_active comes from Users table with a boolean attribute for an an Active

ActiveAdmin.register Degree do

  belongs_to :user, :optional => true

  menu :parent => 'Users'

  config.sort_order = 'users.last_name_asc'

  filter :user_active, :as => :select
  **filter :site_id, collection: -> { User.all }, label: 'sites'**
  filter :degree_type
  filter :major
  filter :institution
  filter :completion_date

I've tried this as well

  filter :site_id, :as => :select, collection: -> { User.all }, label: 'sites'

and no error message

I've also tried something like this with no error message, but nothing in the UI comes through again

filter :site, label: "Site", :as => :select, :collection => User.site_id

I've tried this and get the following errors:

  filter :site_id, label: "Site", :as => :select, :collection => User.all

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

Any help here?

Update

I did some work-around and this is what I end-ed up with

filter :user_site_id, label: "site", :as => :select, :collection => User.all.map{|u| u.site}.map{|s| s.city if s.present? }.uniq.compact

The collection says

  1. Iterated through all the users and call u.site (because a site_id belongs to a User so there's a Site table that'll take each instance of a user)
  2. Since we're now at each Site object, map each of those values and retrieve the city name for each Site object if it's present...
  3. I want to only filter the uniq values and .compact is getting rid of the nil values in the array

I think there's a better solution because I'm going through so many iterations? Anyone have any other ideas? I'm going to make this an instance method on the Users model after the refactor.

1

1 Answers

1
votes

Try this out:

 filter :site_id, as: :select, collection: -> { User.pluck(:site_id) }, label: 'Sites'