gpt4 book ai didi

sql - 当同一模型上有多个联接时,RailsAdmin 过滤器不起作用

转载 作者:行者123 更新时间:2023-12-02 08:23:45 26 4
gpt4 key购买 nike

当一个类与另一个类有多个 belongs_to 关联时,当用户尝试按第二个关联进行过滤时,RailsAdmin 会生成错误的查询。实际例子:

class Issue < ActiveRecord::Base
belongs_to(:author, class_name: 'Worker', foreign_key: 'author_id', inverse_of: :authored_issues)
belongs_to(:assignee, class_name: 'Worker', foreign_key: 'assignee_id', inverse_of: :assigned_issues)
end

class Worker < ActiveRecord::Base
has_many(:authored_issues, class_name: 'Issue', foreign_key: 'author_id', inverse_of: :author)
has_many(:assigned_issues, class_name: 'Issue', foreign_key: 'assignee_id', inverse_of: :assignee)
end

当您访问 /admin/issues 并尝试按受托人过滤时,生成的 SQL 会产生错误的结果,因为它不知道该表已使用别名并按作者过滤:

SELECT  "issues"."id" AS t0_r0, "issues"."title" AS t0_r1, "issues"."description" AS t0_r2, "issues"."author_id" AS t0_r3, "issues"."assignee_id" AS t0_r4, "issues"."created_at" AS t0_r5, "issues"."updated_at" AS t0_r6, "users"."id" AS t1_r0, "users"."name" AS t1_r1, "users"."created_at" AS t1_r2, "users"."updated_at" AS t1_r3, "assignees_issues"."id" AS t2_r0, "assignees_issues"."name" AS t2_r1, "assignees_issues"."created_at" AS t2_r2, "assignees_issues"."updated_at" AS t2_r3
FROM "issues"
LEFT OUTER JOIN "users" ON "users"."id" = "issues"."author_id"
LEFT OUTER JOIN "users" "assignees_issues" ON "assignees_issues"."id" = "issues"."assignee_id"
WHERE ((LOWER(users.name) LIKE '%asdf%')) ORDER BY issues.id desc LIMIT 20 OFFSET 0

如何解决这个问题? WHERE 子句应使用 "assignees_issues".name 。这是 ActiveRecord、RailsAdmin 或我的应用程序上的错误吗?

最佳答案

我必须使用 RailsAdmin DSL 来指定新的别名表名称。基本上:

 config.model 'Issue' do
list do
field :title
field :description
field :author
field :assignee do
searchable do
{'assignees_issues' => 'name'}
end
end
end
end

关于sql - 当同一模型上有多个联接时,RailsAdmin 过滤器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25984009/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com