views:

106

answers:

2

I'm doing my best to bend my brain around arel and the relational algebra behind it, but how to represent a SELECT DISTINCT is consistently eluding my comprehension. Can anyone explain how to arel:

SELECT DISTINCT title FROM posts; 

Many thanks!

+1  A: 

Post.select('DISTINCT title')

Bryan Ash
Not exactly algebraic, but hard to argue with it's efficiency ;-)
jemmons
A: 

The previous answer is the Rails way, no? Not the Arel way.

This works:

posts = Table(:posts)
posts.project(Arel::Distinct.new(posts[:title]))

I'd guess there's another "more correct" way to do this via the API but I haven't figured that out yet.

numbers1311407