0
我有以下表結構(簡體)DISTINCT與Ruby on Rails和阿雷爾
id | version | slug | state
-------------------------------
1 | 1 | A | published
2 | 2 | A | draft
3 | 1 | B | published
4 | 1 | C | published
4 | 2 | C | published
4 | 3 | C | draft
我想獲得最高的版本號的所有記錄,通過蛞蝓和其狀態發佈分組。這樣做的SQL是這樣的:
SELECT DISTINCT ON(slug) *
FROM guides
WHERE state = 'published'
ORDER BY slug ASC, version DESC;
,包裝到Rails:
Klass.select('DISTINCT ON(slug) *').where(state: :published).order(slug: :asc, version: :desc)
有沒有更好的語法是否可用?在過去我使用了一點Arel,但我似乎沒有弄清楚正確的部分。