DISTINCT ON with Ruby on Rails and Arel -


i have following table structure (simplified)

id | version | slug |     state -------------------------------  1 |       1 |    | published  2 |       2 |    |     draft  3 |       1 |    b | published  4 |       1 |    c | published  4 |       2 |    c | published  4 |       3 |    c |     draft 

i want records highest version number, grouped slug , state published. sql looks this:

select distinct on(slug) * guides state = 'published' order slug asc, version desc; 

and packed rails:

klass.select('distinct on(slug) *').where(state: :published).order(slug: :asc, version: :desc) 

is there nicer syntax available? in past used bit of arel, don't seem distinct part correct.

the ar query quite nice , simple. don't think can different , don't see wrong it.

the thing can change use distinct(slug) rather distinct on(slug) *.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -