2

I have a query that looks like this:

 Person.where.not(first_name: nil).where.not("lower(first_name)  = ?", 'mike').order('RANDOM()').limit(6).pluck("distinct(lower(first_name))")

It returns me the following error when I try to run it:

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

How can I solve this problem?

1
  • 1
    try Person.where.not(first_name: nil).where.not("lower(first_name) = ?", 'mike').order('RANDOM()').limit(6).pluck("lower(first_name)").uniq or Person.where('first_name IS NOT NULL and lower(first_name) != ?', 'mike').order('RANDOM()').limit(6).pluck("lower(first_name)").uniq Commented Jun 1, 2015 at 6:38

1 Answer 1

1
Person.where.not(first_name: nil).where.not("lower(first_name)  = ?", 'mike').order('RANDOM()').limit(6).select("lower(first_name)").uniq

Since pluck will fire extra query so instead of pluck you should use select

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.