【问题标题】:rails_admin gem filter with has_many_through associationrails_admin gem 过滤器与 has_many_through 关联
【发布时间】:2017-06-06 12:37:32
【问题描述】:

在我的应用程序中

产品通过 category_has_products 有很多类别

Category 通过 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

【问题讨论】:

    标签: rails-admin


    【解决方案1】:

    我找到答案了

    在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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多