rails_admin gem filter with has_many_through association

会有一股神秘感。 提交于 2020-01-17 08:30:49

问题


In my application

Product has many categories though category_has_products

Category has many products though category_has_products

Initially I used

at product.rb

default_scope { includes(:brand, :categories) }

and in rails_admin config set it as

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

This works like a charm. This got in to performance issue due to has_many_through(other pages using product details got affected)

So I removed the default_scope from product.

After that I am getting

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

any idea on how to preload categories?

Note: rails_admin gem version is 0.8.1 only I am not able to update to latest version 1.X.X


回答1:


I found answer

in 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]

To add filter option

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

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



来源:https://stackoverflow.com/questions/44390218/rails-admin-gem-filter-with-has-many-through-association

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!