Created
June 9, 2011 21:05
-
-
Save ernie/1017744 to your computer and use it in GitHub Desktop.
Attempt #1 at cross-DB select distinct
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# So, a SELECT DISTINCT on some DBs requires that all order values are in the select clause. | |
# I'm hoping there's a better cross-platform way of doing this without branching, and I | |
# kind of think I must be overlooking something that's already built-in. | |
# Open to suggestions! | |
def distinct_relation(relation) | |
offset, limit = relation.offset_value, relation.limit_value | |
klass.select('DISTINCT subquery.*'). | |
from(Arel::Nodes::As.new( | |
Arel::Nodes::Grouping.new(relation.except(:offset, :limit).arel.ast), | |
Arel.sql('subquery') | |
)) | |
.offset(offset).limit(limit) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment