You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Per Postgres docs - and experimentation! ;) - specify that the DISTINCT ON clause must have a matching ORDER BY, such that the columns used in the distinct clause are the left-most in the ORDER clause. So you end up needing to do:
Model.distinct_on(:col).order(:col)
It would be nice (I think?) if this was handled automagically. (Or is this supposed to be automagically handled by AREL?)
Bonus - This plays weird with other things that apply ordering (ActiveAdmin). My guess (and hope) is that there's a way to say "no, this ordering always is left-most", so that when something like AA adds their ordering, it doesn't break the DISTINCT ON.
The text was updated successfully, but these errors were encountered:
rangerscience
changed the title
DISTINCT ON require ORDER
DISTINCT ON requires matching ORDER clause
Jun 30, 2023
Per Postgres docs - and experimentation! ;) - specify that the
DISTINCT ON
clause must have a matchingORDER BY
, such that the columns used in the distinct clause are the left-most in the ORDER clause. So you end up needing to do:Model.distinct_on(:col).order(:col)
It would be nice (I think?) if this was handled automagically. (Or is this supposed to be automagically handled by AREL?)
Bonus - This plays weird with other things that apply ordering (ActiveAdmin). My guess (and hope) is that there's a way to say "no, this ordering always is left-most", so that when something like AA adds their ordering, it doesn't break the
DISTINCT ON
.The text was updated successfully, but these errors were encountered: