在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
在得分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
引入的左联接,它可以再次指定scores_export_orders
表。我不知道为什么要在这里而不是在控制台上引入它。
答案 0 :(得分:0)
已解决。我不知道我已经在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
范围内进行双重联接,而无需在过滤器范围内再次联接。