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

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -