Efficiently find next/previous ActiveRecord records using keyset pagination in a single query.
OrderQuery is a Ruby gem that provides efficient navigation through ActiveRecord records using keyset pagination. It solves the performance problems of traditional offset-based pagination by generating optimized SQL queries that find next/previous records based on column values rather than row offsets. This approach eliminates the performance degradation that occurs with large datasets in offset-based pagination.
Ruby on Rails developers who need efficient pagination for navigation interfaces, infinite scrolling, or record navigation features in applications with large datasets. Database performance-conscious developers who want to avoid OFFSET-based pagination problems.
Developers choose OrderQuery because it provides database-level performance optimization for pagination through keyset techniques, offers a clean ActiveRecord-like API for navigation, and handles complex multi-column ordering scenarios that would be cumbersome to implement manually with raw SQL.
Find next / previous Active Record(s) in one query
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Uses column comparisons instead of OFFSET, eliminating performance degradation on large datasets, as shown in the SQL query examples and references to 'use-the-index-luke.com'.
Supports complex ordering with arrays of values and custom SQL, enabling advanced sorting like priority levels or calculated fields, as demonstrated in the advanced example.
Integrates cleanly with Rails models through scopes and instance methods, making it easy to adopt with methods like order_home_at for navigation.
Provides before, after, previous, next, and position methods, directly useful for building interfaces like infinite scroll or admin record navigation.
Only works with ActiveRecord, so it's not suitable for projects using other ORMs like Sequel or different database abstractions.
Setting up order queries requires careful specification of unique columns and handling of null values, which can be verbose and error-prone for beginners.
Designed for sequential navigation, making it less ideal for use cases that require direct access to arbitrary pages without previous context.
Generated SQL queries are complex with OR conditions and wrapping, which can affect readability and debugging, as noted in the performance optimization section.