【发布时间】:2018-03-14 19:18:54
【问题描述】:
在 Rails admin 中搜索时,无法弄清楚如何过滤嵌套关联。
我在模型的 rails_admin 配置中有以下代码。
class Painting < ApplicationRecord
...
rails_admin do
configure :translations, :globalize_tabs
create do
field :author do
searchable [:last_name]
queryable [:last_name]
filterable true
end
field :genre
field :technique
field :material
field :active
field :painting_images
field :author_preview
field :translations
end
end
我尝试了模型中的所有配置。但没有任何效果。我在此下拉输入中输入的任何内容都将被忽略。
我在 puma 服务器中有以下日志。
Started GET "/admin/author?associated_collection=author&compact=true¤t_action=create&source_abstract_model=painting&query=George" for 127.0.0.1 at 2018-03-14 22:07:11 +0300
Processing by RailsAdmin::MainController#index as JSON
Parameters: {"associated_collection"=>"author", "compact"=>"true", "current_action"=>"create", "source_abstract_model"=>"painting", "query"=>"George", "model_name"=>"author"}
Admin Load (0.3ms) SELECT "admins".* FROM "admins" WHERE "admins"."id" = $1 ORDER BY "admins"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
Painting Load (0.3ms) SELECT "paintings".* FROM "paintings" WHERE "paintings"."id" IS NULL ORDER BY "paintings"."id" DESC LIMIT $1 [["LIMIT", 1]]
(0.3ms) SELECT COUNT(*) FROM "authors"
Author Load (0.4ms) SELECT "authors".* FROM "authors" ORDER BY authors.id desc LIMIT $1 [["LIMIT", 30]]
Author::Translation Load (0.6ms) SELECT "author_translations".* FROM "author_translations" WHERE "author_translations"."author_id" IN (114, 113, 112, 111, 110, 109, 108, 107, 106, 105, 104, 103, 102, 101, 100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85)
Completed 200 OK in 61ms (Views: 1.4ms | ActiveRecord: 1.9ms)
问题是我在输入中输入的所有内容都在参数中进行处理,但从未得到评估。所以它只是被忽略了,它不会通过params[:query]过滤关联的集合
我使用 globalize gem 进行翻译,但不知道如何正确配置 rails admin 以使用它的过滤器。
我在原始存储库中进行了搜索。 我觉得有点不对this
def get_collection(model_config, scope, pagination)
...
options = options.merge(query: params[:query]) if params[:query].present?
...
model_config.abstract_model.all(options, scope)
end
此查询在此处可用,但在此处被忽略。
我试着用
model_config.abstract_model.all(options, scope).where(last_name: params[:query])
这是有效的。但这绝对是错误的。有人可以帮助我用更通用的方法解决这个问题吗?
【问题讨论】:
-
作者的姓氏是否显示在下拉列表中?如果不是,则需要为 RailsAdmin 重新定义
author_name方法 -
我发现了一个丑陋的黑客来解决这个问题。如果有人会遇到这个问题,我可以提供不是很好的解决方案。
标签: ruby-on-rails rails-admin globalize