[英]Activeadmin custom filter fails
在activeadmin中,我们需要在Score模型的索引页面上应用一种复杂的过滤器。 即,我们希望在Score
表中获取结果,以使它们的ExportOrder
具有选定的DistributionChain
。 我们从以下模型开始:
class Score < ApplicationRecord
has_and_belongs_to_many :export_orders, join_table: :scores_export_orders
end
class ExportOrder < ApplicationRecord
belongs_to :distribution_chain
has_and_belongs_to_many :scores, join_table: :scores_export_orders
end
class DistributionChain < ApplicationRecord
has_many :export_orders
end
在schema.rb(摘录)中:
create_table "scores_export_orders", id: false, force: :cascade do |t|
t.integer "score_id"
t.integer "export_order_id"
t.index ["export_order_id"], name: "index_scores_export_orders_on_export_order_id", using: :btree
t.index ["score_id"], name: "index_scores_export_orders_on_score_id", using: :btree
end
create_table "export_orders", force: :cascade do |t|
t.integer "distribution_chain_id"
end
在Score Activeadmin中:
ActiveAdmin.register Score
filter :delivery_distr_chain, as: :select, collection: DistributionChain.all
end
筛选器的范围在分数模型中定义:
class Score < ApplicationRecord
...
scope :delivery_distr_chain, -> (input) {
self.joins(:export_orders).where('export_orders.distribution_chain_id = ?', input)
}
def self.ransackable_scopes(auth_object = nil)
[:delivery_distr_chain]
end
end
以下是错误:
ActionView::Template::Error (PG::DuplicateAlias: ERROR: table name "scores_export_orders" specified more than once
: SELECT COUNT(DISTINCT count_column) FROM (SELECT DISTINCT "scores"."id" AS count_column FROM "scores" INNER JOIN "scores_export_orders" ON "scores_export_orders"."score_id" = "scores"."id" INNER JOIN "export_orders" ON "export_orders"."id" = "scores_export_orders"."export_order_id" LEFT JOIN scores_export_orders ON scores.id = scores_export_orders.score_id WHERE (NOT (scores_export_orders.score_id IS NULL)) AND (export_orders.distribution_chain_id = '2') LIMIT $1 OFFSET $2) subquery_for_count):
1: insert_tag renderer_for(:index)
activerecord (5.0.7) lib/active_record/connection_adapters/postgresql_adapter.rb:600:in `async_exec'
我感觉查询未正确编写。 您能告诉我们我们哪里失败了吗?
编辑:值得注意的是,在rails控制台中和在Score
模型中的范围内执行查询之间的区别。
如果我在Rails控制台中执行相同的查询,那么它将正常工作而不会出错。 Score.joins(:export_orders).where('export_orders.distribution_chain_id = ?', '2').to_sql
为:
"SELECT \\"scores\\".* FROM \\"scores\\" INNER JOIN \\"scores_export_orders\\" ON \\"scores_export_orders\\".\\"score_id\\" = \\"scores\\".\\"id\\" INNER JOIN \\"export_orders\\" ON \\"export_orders\\".\\"id\\" = \\"scores_export_orders\\".\\"export_order_id\\" WHERE (export_orders.distribution_chain_id = '2') ORDER BY \\"scores\\".\\"created_at\\" DESC"
而完全相同的查询在Score
模型的作用域中失败, .to_sql
输出为:
"SELECT DISTINCT \\"scores\\".* FROM \\"scores\\" INNER JOIN \\"scores_export_orders\\" ON \\"scores_export_orders\\".\\"score_id\\" = \\"scores\\".\\"id\\" INNER JOIN \\"export_orders\\" ON \\"export_orders\\".\\"id\\" = \\"scores_export_orders\\".\\"export_order_id\\" LEFT JOIN scores_export_orders ON scores.id = scores_export_orders.score_id WHERE (NOT (scores_export_orders.score_id IS NULL)) AND (export_orders.distribution_chain_id = '2') ORDER BY \\"scores\\".\\"id\\" desc"
主要区别在于范围引入的LEFT JOIN scores_export_orders ON scores.id = scores_export_orders.score_id
: LEFT JOIN scores_export_orders ON scores.id = scores_export_orders.score_id
,它可以再次指定scores_export_orders
表。 我不知道为什么在这里而不是在控制台上引入它...
解决了。 我不知道我已经在我的ActiveAdmin for Scores中声明了以下范围:
scope :exported, -> { joins("LEFT JOIN scores_export_orders ON scores.id = scores_export_orders.score_id").where.not("scores_export_orders.score_id IS NULL").distinct
在ActiveAdmin中称为
controller do
def scoped_collection
end_of_association_chain.exported
end
...
end
现在,我重新编写了分数模型中的代码:
class Score < ApplicationRecord
...
scope :exported, -> { joins("INNER JOIN scores_export_orders ON scores.id = scores_export_orders.score_id INNER JOIN export_orders ON export_orders.id = scores_export_orders.export_order_id").where.not("scores_export_orders.score_id IS NULL").distinct }
...
scope :delivery_distr_chain, -> (input) {
self.where('export_orders.distribution_chain_id = ?', input)
}
def self.ransackable_scopes(auth_object = nil)
[:delivery_distr_chain]
end
end
这样我就可以在exported
作用域上进行双重联接,而无需在过滤器作用域上再次联接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.