选择偏移量> 0的最大值将返回nil

时间:2022-11-20 15:41:14

I would like to use fresh_when last_modified: @conversations.maximum(:updated_at) with pagination. However, when @conversations is on page 2, updated_at is being returned as nil even though there is one record in the set.

我想使用last_when last_modified:@ conversations.maximum(:updated_at)和分页。但是,当@conversations在第2页时,即使集合中有一条记录,updated_at也会返回为nil。

irb(mail):001:0> conversations = conversations.paginate(page: 1)
=> [10 conversations]

irb(main):002:0> conversations.maximum(:updated_at)
   (3.0ms)  SELECT MAX(`inbox_conversations`.`updated_at`) AS max_id FROM `inbox_conversations` LIMIT 10 OFFSET 0
=> 2013-01-09 05:12:10 UTC # Correct

irb(main):003:0> conversations = conversations.paginate(page: 2)
=> [10 conversations]

irb(main):004:0> conversations.maximum(:updated_at)
   (2.7ms)  SELECT MAX(`inbox_conversations`.`updated_at`) AS max_id FROM `inbox_conversations` LIMIT 10 OFFSET 10
=> nil # Not correct

2 个解决方案

#1


2  

Rails is misinterpreting what you want

Rails误解了你想要的东西

SELECT MAX(`inbox_conversations`.`updated_at`) AS max_id FROM `inbox_conversations` LIMIT 10 OFFSET 10

Means run the query and then take the first 10 results from offset 10. This returns nil because the result set has a single row - the maximum updated_at. What you wanted instead was the offset limit to affect the rows over which the maximum was calculated.

means运行查询,然后从offset 10获取前10个结果。返回nil,因为结果集有一行 - 最大updated_at。您想要的是偏移限制,以影响计算最大值的行。

You could do this with a subquery, but I'd just do this calculation in ruby since you're loading the rows anyway, using the regular Enunerable methods. Something like

您可以使用子查询执行此操作,但我只是在ruby中执行此计算,因为您无论如何都要使用常规的Enunerable方法加载行。就像是

conversations.map(&:updated_at).max

#2


1  

SQL MAX query does not work with OFFSET (plain SQL workaround)

SQL MAX查询不适用于OFFSET(纯SQL解决方法)

Any ActiveRecord .offset(n).maximum(:field) gives nil if n > 0

如果n> 0,则任何ActiveRecord .offset(n).maximum(:field)都给出nil

Try to use plain Ruby as a workaround (it saves you an query in advance).

尝试使用纯Ruby作为一种解决方法(它可以提前为您保存查询)。

conversations.max_by(&:updated_at)

#1


2  

Rails is misinterpreting what you want

Rails误解了你想要的东西

SELECT MAX(`inbox_conversations`.`updated_at`) AS max_id FROM `inbox_conversations` LIMIT 10 OFFSET 10

Means run the query and then take the first 10 results from offset 10. This returns nil because the result set has a single row - the maximum updated_at. What you wanted instead was the offset limit to affect the rows over which the maximum was calculated.

means运行查询,然后从offset 10获取前10个结果。返回nil,因为结果集有一行 - 最大updated_at。您想要的是偏移限制,以影响计算最大值的行。

You could do this with a subquery, but I'd just do this calculation in ruby since you're loading the rows anyway, using the regular Enunerable methods. Something like

您可以使用子查询执行此操作,但我只是在ruby中执行此计算,因为您无论如何都要使用常规的Enunerable方法加载行。就像是

conversations.map(&:updated_at).max

#2


1  

SQL MAX query does not work with OFFSET (plain SQL workaround)

SQL MAX查询不适用于OFFSET(纯SQL解决方法)

Any ActiveRecord .offset(n).maximum(:field) gives nil if n > 0

如果n> 0,则任何ActiveRecord .offset(n).maximum(:field)都给出nil

Try to use plain Ruby as a workaround (it saves you an query in advance).

尝试使用纯Ruby作为一种解决方法(它可以提前为您保存查询)。

conversations.max_by(&:updated_at)