当一个类与另一个类有多个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