百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
O

order_query

> 后端框架
开源

在一个查询中查找下一个/上一个活动记录

516 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

在一个查询中查找下一个/上一个活动记录

order_query Build Status Coverage Status

This gem finds the next or previous record(s) relative to the current one efficiently using keyset pagination, e.g. for navigation or infinite scroll.

Installation

Add to Gemfile:

ruby
gem 'order_query', '~> 0.5.6'

Usage

Use order_query(scope_name, *order_option) to create scopes and class methods in your model and specify how you want results ordered. A basic example:

ruby
class Post  #
Post.published.order_home_reverse #=> #

Before / after, previous / next, and position

First, get an OrderQuery::Point for the record:

ruby
p = Post.published.order_home_at(Post.find(31)) #=> #

It exposes these finder methods:

ruby
p.before   #=> #
p.after    #=> #
p.previous #=> #
p.next     #=> #
p.position #=> 5

The before and after methods also accept a boolean argument that indicates whether the relation should exclude the given point or not. By default the given point is excluded, if you want to include it, use before(false) / after(false).

If you want to obtain only a chunk (i.e., a page), use before or after with ActiveRecord's limit method:

ruby
p.after.limit(20) #=> #

Looping to the first / last record is enabled for next / previous by default. Pass false to disable:

ruby
p = Post.order_home_at(Post.order_home.first)
p.previous        #=> #
p.previous(false) #=> nil

Even with looping, nil will be returned if there is only one record.

You can also get an OrderQuery::Point from an instance and a scope:

ruby
posts = Post.published
post  = posts.find(42)
post.order_home(posts) #=> #

Dynamic columns

Query with dynamic order columns using the seek(*order) class method:

ruby
space = Post.visible.seek([:id, :desc]) #=> #

This returns an OrderQuery::Space that exposes these methods:

ruby
space.scope           #=> #
space.scope_reverse   #=> #
space.first           #=> scope.first
space.last            #=> scope_reverse.first
space.at(Post.first)  #=> #

OrderQuery::Space is also available for defined order_queries:

ruby
Post.visible.order_home_space #=> #

Alternatively, get an OrderQuery::Point using the seek(scope, *order) instance method:

ruby
Post.find(42).seek(Post.visible, [:id, :desc]) #=> #
# scope defaults to Post.all
Post.find(42).seek([:id, :desc]) #=> #

Advanced example

…

How it works

Internally this gem builds a query that depends on the current record's values and looks like this:

sql
-- Current post: pinned=true published_at='2014-03-21 15:01:35.064096' id=9
SELECT "posts".* FROM "posts"  WHERE
  ("posts"."pinned" = 'f' OR
   "posts"."pinned" = 't' AND (
      "posts"."published_at" < '2014-03-21 15:01:35.064096' OR
      "posts"."published_at" = '2014-03-21 15:01:35.064096' AND "posts"."id" < 9))
ORDER BY
  "posts"."pinned"='t' DESC, "posts"."pinned"='f' DESC,
  "posts"."published_at" DESC,
  "posts"."id" DESC
LIMIT 1

The actual query is a bit different because order_query wraps the top-level OR with a (redundant) non-strict column x0' AND (x0 OR ...) for performance reasons. This can be disabled with OrderQuery.wrap_top_level_or = false.

See the implementation in sql/where.rb.

See how this affects query planning in Markus Winand's slides on Pagination done the Right Way.

This project uses MIT license.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rubyactiverecordkeyset-pagingpaginationrails

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类后端框架
定价开源

> 相关工具

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架