Ruby on rails 考虑到多个属性,范围按更大数量划分
在Rails(6)应用程序中,我的Ruby on rails 考虑到多个属性,范围按更大数量划分,ruby-on-rails,postgresql,Ruby On Rails,Postgresql,在Rails(6)应用程序中,我的课程模型具有以下属性 主题(字符串) 房间(线) 版本(整数) 现在,我正在尝试创建一个带有返回的范围,为每个主题+房间二人组创建一个版本最丰富的记录 因此,在数据库中有以下记录 |subject|room |edition| | math | A001| 1 | | math | A001| 2 | | math | A002| 1 | | chem | A002| 1 | | chem | A002| 2
课程
模型具有以下属性
- 主题(字符串)
- 房间(线)
- 版本(整数)
范围
,为每个主题
+房间
二人组创建一个版本最丰富的记录
因此,在数据库中有以下记录
|subject|room |edition|
| math | A001| 1 |
| math | A001| 2 |
| math | A002| 1 |
| chem | A002| 1 |
| chem | A002| 2 |
| chem | A002| 3 |
课程。当前
返回
[<Course subject: 'math', room: 'A001', edition: 2>,
<Course subject: 'math', room: 'A002', edition: 1>,
<Course subject: 'chem', room: 'A002', edition: 3>]
[,,
,
]
我认为这应该有效:
def self.current
Course.select('max(edition) as edition, subject, room').group('subject, room')
end