A Ruby gem providing cursor-based pagination for ActiveRecord relations with support for custom ordering.
ActiveRecordCursorPaginate is a Ruby gem that adds cursor-based pagination to ActiveRecord relations. It solves the problem of unstable pagination in traditional offset/limit approaches by using encoded cursors that uniquely identify records, ensuring consistent results even when data changes between page requests.
Ruby on Rails developers building applications that require efficient, stable pagination through large or frequently updated datasets, such as APIs, dashboards, or data-intensive features.
Developers choose this gem for its robust handling of dynamic data, support for custom ordering, and performance optimizations through proper indexing, making it a reliable alternative to offset-based pagination in ActiveRecord.
Cursor-based pagination for ActiveRecord
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 encoded cursors to uniquely identify rows, preventing result jumps when records are added or deleted during pagination, as detailed in the 'How does it work?' section.
Allows ordering by any column, multiple columns, or custom SQL expressions via Arel.sql, with implicit primary key appending for uniqueness unless disabled.
Supports forward pagination with 'after' cursors and backward pagination with 'before' cursors, enabling easy iteration over pages in both directions.
Leverages database indexes for ordered columns; the README provides clear examples for adding compound indexes (e.g., on author and id) to optimize query efficiency.
Requires adding specific database indexes for custom order columns to maintain performance, which adds complexity to schema management and migrations.
Only supports Ruby 2.7+ and Rails 7.0+, excluding teams on older versions without a straightforward upgrade path mentioned in the README.
Ordering by JOINed columns or custom SQL expressions requires manual SQL qualification and Arel.sql wrapping, increasing the risk of errors and potential performance degradation if not indexed properly.