我的模型是:
service_provider has_many questions, question belongs_to service_provider
在 Activeadmin 中,我想索引我的 service_providers 并按他们的 questions_count 对它们进行排序。
所以,要获得一个字段,我可以排序,我定义我的 default_scope 如下:
scope "all", :default => true do |sp|
sp.joins("LEFT JOIN questions on questions.service_provider_id = service_providers.id")
.select("service_providers.*, COUNT(service_provider_id) as questions_count")
.group("service_providers.id")
end
我的列定义如下所示:
column :questions, :sortable => "questions_count" do |sp|
sp.questions_count
end
索引我的服务提供商时,一切看起来都很好。但是,当我按他们的 questions_count 进行排序时,我收到以下错误:
SQLite3::SQLException: no such column: service_providers.questions_count: SELECT service_providers.*, COUNT(service_provider_id) as questions_count FROM "service_providers" LEFT JOIN questions on questions.service_provider_id = service_providers.id GROUP BY service_providers.id ORDER BY "service_providers"."questions_count" desc LIMIT 30 OFFSET 0
因此,他尝试 ORDER BY "service_providers"."questions_count" desc。事实上,他应该 ORDER BY "questions_count" desc。
有什么建议,我怎么能告诉他在他的订单声明中使用正确的字段名?