PG :: InvalidColumnReference:错误:用于Ruby on Rails应用程序中的SELECT DISTINCT

时间:2015-06-01 06:35:09

标签: sql ruby-on-rails ruby activerecord

我有一个如下所示的查询:

 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...

我该如何解决这个问题?

1 个答案:

答案 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