rails_admin gem 过滤器与 has_many_through 关联

rails_admin gem filter with has_many_through association

在我的申请中

Product has many categories through category_has_products

Category has many products through category_has_products

最初,我使用

在 product.rb

default_scope { includes(:brand, :categories) }

并在 rails_admin 配置中将其设置为

field :categories, :string do
 searchable [{Category => :name}]
end  

这很有魅力。由于 has_many_through(其他使用产品详细信息的页面受到影响)

,这导致了性能问题

所以我从产品中删除了 default_scope。

在那之后,我得到

PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "categories"

知道如何预加载类别吗?

注意:rails_admin gem 版本仅为 0.8.1。我无法更新到最新版本 1.X.X

我找到了答案

在product.rb

scope :include_categories, -> {includes(:categories)}
class << self
  alias_method :all_products, :include_categories
end  

admin_product_config.rb

from
    scopes %i[all brandless unmatched matched ignored fresh diy_only]
to
    scopes %i[all_products brandless unmatched matched ignored fresh diy_only]

添加过滤器选项

  field :categories, :string do
    searchable [{Category => :name}]
  end      
  configure :categories do
    hide
  end 

谢谢@jxpx777 https://github.com/sferik/rails_admin/issues/1348#issuecomment-39373394