3
标题道歉,因为我不知道如何简洁地描述我的问题。ActiveRecord根据自定义唯一性和最大列值进行选择
我们假设我们在数据库中有多个Item
实例,并包含以下列:name
,year
和version
。下面的示例:
| id | name | year | version |
|----|-----------|--------|---------|
| 1 | Blockhead | 2010 | 1 |
| 2 | Blockhead | 2010 | 2 |
| 3 | Jughead | 2011 | 1 |
我只想返回结果其中名字和年份是唯一的,但只返回最新版本的名称/年的独特组合。即,我想回到:
| id | name | year | version |
|----|-----------|--------|---------|
| 2 | Blockhead | 2010 | 2 |
| 3 | Jughead | 2011 | 1 |
如果可能的话,我想没有一个分组数据集...但我的SQL知识/ AR在这方面是有限的,我不知道是什么正确的解决方案将是。我目前检索所有记录,然后筛选出我不想要的记录,但这是一个不合理的解决方案。
@items = ::Item.active.paginate(per_page: 30, page: page || 1).to_a
# Do not show a given item unless it's the latest version
@items.delete_if do |item|
@items.any? do |other|
other.id != item.id &&
other.read_attribute(:name) == item.read_attribute(:name) &&
other.year == item.year &&
other.version > item.version
end
end