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
Post a Comment