I'm trying to combine a uniq
statement with a select("distinct")
statement in Active Record, and it results in two DISTINCT
keywords, which of course leads to an invalid query. This is the simplest example I have come up with. (Mark that is is simplified in order to help you understand the problem - I'm not simply asking for how I get out distinct ids from a database.)
Product.all.uniq.select("distinct id").map(&:id)
This gives me this error message:
Product Load (0.7ms) SELECT DISTINCT distinct id FROM "products"
ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR: syntax error at or near "distinct"
LINE 1: SELECT DISTINCT distinct id FROM "products"
^
: SELECT DISTINCT distinct id FROM "products"
Why do I get two DISTRINCT
keywords here? Is there any way to avoid it? Using uniq
twice works, but I need to do a select for one of the filters I'm implementing.
Edit: The select("distinct..")
has to go before the uniq
statement.