首页 > 解决方案 > rails - active admin - PG::InvalidColumnReference: ERROR: SELECT DISTINCT ON 表达式必须匹配初始 ORDER BY 表达式

问题描述

我在模型中有一个范围,当我从 rails 控制台调用它时它工作得非常好,但是当我尝试在活动管理员中使用它时,它会抛出一个错误

这是范围

scope :clicked, -> { select('distinct on (email_stats.clicked_url) email_stats.*').order(:clicked_url, :action_performed_at)}

这是我尝试在活动管理员中使用它时遇到的错误

ActionView::Template::Error (PG::InvalidColumnReference: ERROR: SELECT DISTINCT ON 表达式必须匹配初始 ORDER BY 表达式 LINE 1: SELECT distinct on (email_stats.clicked_url) email_stats.* ... ^ : SELECT distinct on (email_stats. clicked_url) email_stats.* FROM “email_stats” ORDER BY “email_stats”.“id” desc, “email_stats”.“clicked_url” ASC, “email_stats”.“action_performed_at” ASC LIMIT $1 OFFSET $2):

谁能让我知道我在这里缺少什么?我一直在寻找其他帖子,但它们似乎都没有帮助。

标签: ruby-on-railsactiveadmin

解决方案


尝试在范围内使用 distinct 可能会有问题。相反,我使用:

controller do
  def apply_filtering(collection)
    super.distinct
  end
end

推荐阅读