我有一个如下所示的查询:
Person.where.not(first_name: nil).where.not("lower(first_name) = ?", 'mike').order('RANDOM()').limit(6).pluck("distinct(lower(first_name))")
当我尝试运行它时,它返回以下错误:
ActiveRecord::StatementInvalid: PG::InvalidColumnReference: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list
LINE 1: ...AND (NOT (lower(first_name) = 'mike')) ORDER BY RANDOM() L...
我该如何解决这个问题?
答案 0 :(得分:1)
Person.where.not(first_name: nil).where.not("lower(first_name) = ?", 'mike').order('RANDOM()').limit(6).select("lower(first_name)").uniq
因为pluck会触发额外的查询,所以不应该使用select