0
votes

I have an ActiveRecord class Foo and I have an ActiveAdmin index view listing all my foos. Foo has a scope called bland and my UI requirement is that I have a checkbox in the Filters section so that I can see all the bland foos.

I.e., I'd like the Filters section to look like this:

enter image description here

I have read through questions and blogs like the following that say I need a custom ransacker:

Use ActiveRecord scope in ActiveAdmin filter

So my code is something like this:

ActiveAdmin.register Foo
  filter :bland, label: 'Bland Foos', as: :check_boxes, collection: ['exclude tasty']
end

class Foo < ApplicationRecord
  scope :bland, -> { where("tasty < 3") }
  def self.ransackable_scopes(_auth_object = nil)
    [:bland]
  end
end

However, when I try to run this I get an error like:

undefined method `bland_in' for #Ransack::Search:0x00007f8d18e61db8

Ouch.

As an experiment I tried something like this:

ActiveAdmin.register Foo
  filter :bland, label: 'Bland Foos'
end

class Foo < ApplicationRecord
  scope :bland, -> (dummy_param_not_used) { where("tasty < 3") }
  def self.ransackable_scopes(_auth_object = nil)
    [:bland]
  end
end

My idea here being: ok, ActiveAdmin will put up a string input and we'll fill in a value just to trigger the code ... but then the filter doesn't even show on my UI!

1
Should that be collection: [['exclude', false], ['tasty',true]] ? - Piers C
Hm ... maybe it would be [['exclude tasty'],true] meaning that I want to have "exclude tasty" as the text. I'll update the question to include a screenshot. Though I tried this and I still get the same error. - Purplejacket

1 Answers

0
votes

See if this can be of any use. You can change UI elements if required.

ActiveAdmin.register Foo
  ......
  scope :bland, -> { where("tasty < 3") }
  ......
end

Reference : ActiveAdmin Documentation