Configuring the has_many filter through Active Admin

I'm trying to just enable category filtering on the Locations page for ActiveAdmin.

I have three models:

class Location < ActiveRecord::Base
  has_many :categories_locations
  has_many :categories, :through => :categories_locations

class CategoriesLocation < ActiveRecord::Base
    belongs_to :category
    belongs_to :location
end

class Category < ActiveRecord::Base
    has_many :categories_locations
    has_many :locations, :through => :categories_locations
end

On my locations page, I use this filter:

ActiveAdmin.register Location do
  filter :name
  filter :category, :collection => proc { Category.all }, :as => :select

However, he continues to throw an error.

undefined method `category_eq' for #<MetaSearch::Searches::Location:0x007fd4f9b965d8>

I tried filter: categories, filter: categories_locations, but nothing works.

Has anyone experienced this - does anyone have a solution?

+3
source share
3 answers

at some point has_many / through is more flexible than habtm (you may have additional fields, etc.)

+1
source

why don't you use habtm?

class Location < ActiveRecord::Base
  has_and_belongs_to_many :categories

class CategoriesLocation < ActiveRecord::Base
end

class Category < ActiveRecord::Base
    has_and_belongs_to_many :locations
end

and then

ActiveAdmin.register Location do
  filter :name
  filter :category_id, :collection => proc { Category.all }, :as => :select
0
source

, , , sql!

Active Admin?

0

All Articles