Ruby on rails 使用Active Admin设置多通过滤器
我试图简单地允许在ActiveAdmin的位置页面上过滤类别 我有三种型号:Ruby on rails 使用Active Admin设置多通过滤器,ruby-on-rails,ruby,activeadmin,meta-search,Ruby On Rails,Ruby,Activeadmin,Meta Search,我试图简单地允许在ActiveAdmin的位置页面上过滤类别 我有三种型号: class Location < ActiveRecord::Base has_many :categories_locations has_many :categories, :through => :categories_locations class CategoriesLocation < ActiveRecord::Base belongs_to :category
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
但是,它不断抛出一个错误
undefined method `category_eq' for #<MetaSearch::Searches::Location:0x007fd4f9b965d8>
未定义的方法'category_eq'#
我尝试过filter:categories,filter:categories\u Location,但没有任何效果
有人经历过这种情况吗?有人有解决方案吗?为什么不使用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
在某些情况下,has\u many/through比habtm更灵活(您可以有其他字段等)如果您可以在sql中编写has many,可以在此处找到答案
我也在寻找相同的解决方案,并找到了如下工作方案。在这里发布,以便将来帮助他人 app/admin/location.rb
ActiveAdmin.register Location do
filter :filter_by_category, label: 'Category', as: :select, collection: Category.pluck(:name, :id)
class Location < ActiveRecord::Base
has_many :categories_locations
has_many :categories, :through => :categories_locations
def self.filter_by_category(category_id)
category_id = 1 if category_id == true # this is required only for id=1, ActiveAdmin return it as `true`
joins(:categories).where("categories.id = ?", category_id)
end
# Add your custom method as ransack
def self.ransackable_scopes(_auth_object = nil)
[:filter_by_category]
end
end
app/model/location.rb
ActiveAdmin.register Location do
filter :filter_by_category, label: 'Category', as: :select, collection: Category.pluck(:name, :id)
class Location < ActiveRecord::Base
has_many :categories_locations
has_many :categories, :through => :categories_locations
def self.filter_by_category(category_id)
category_id = 1 if category_id == true # this is required only for id=1, ActiveAdmin return it as `true`
joins(:categories).where("categories.id = ?", category_id)
end
# Add your custom method as ransack
def self.ransackable_scopes(_auth_object = nil)
[:filter_by_category]
end
end
类位置:类别\u位置
按类别定义自我筛选(类别id)
category_id=1如果category_id==true#这仅对id=1是必需的,ActiveAdmin将其作为“true”返回`
连接(:categories)。其中(“categories.id=?”,categories\u id)
结束
#将自定义方法添加为ransack
定义自身可搜索范围(_auth_object=nil)
[:按类别过滤]
结束
结束
希望对你有帮助 我也有同样的问题,我不能从habtm转到HASU many/through,所以有什么真正的解决办法吗?