Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 考虑到多个属性,范围按更大数量划分_Ruby On Rails_Postgresql - Fatal编程技术网

Ruby on rails 考虑到多个属性,范围按更大数量划分

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

在Rails(6)应用程序中,我的
课程
模型具有以下属性

  • 主题(字符串)
  • 房间(线)
  • 版本(整数)
现在,我正在尝试创建一个带有返回的
范围
,为每个
主题
+
房间
二人组创建一个
版本最丰富的记录

因此,在数据库中有以下记录

|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