# What would be the best way to get next and previous record?

**URL:** <https://www.ruby-forum.com/t/what-would-be-the-best-way-to-get-next-and-previous-record/140097>\
**Category:** Rails\
**Created:** [June 10, 2008, 8:32pm UTC](https://www.ruby-forum.com/t/what-would-be-the-best-way-to-get-next-and-previous-record/140097 "2008-06-10T20:32:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [June 10, 2008, 8:32pm UTC](https://www.ruby-forum.com/t/what-would-be-the-best-way-to-get-next-and-previous-record/140097/1 "2008-06-10T20:32:37Z")

</div>

Let’s say I have a class Post and I do a simple find for a record,  
like Post.find(16). Given this AR instance what would be the best way  
to get the previous and next records? I can’t just use ids (15, 17 in  
this example) because records can be deleted.

Am I asking AR to do too much here and craft some custom SQL to handle  
this?

Thanks for the help.  
Mike

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [June 10, 2008, 8:50pm UTC](https://www.ruby-forum.com/t/what-would-be-the-best-way-to-get-next-and-previous-record/140097/2 "2008-06-10T20:50:21Z")

</div>

found it!

[http://svn.viney.net.nz/things/rails/plugins/acts\_as\_ordered/README](http://svn.viney.net.nz/things/rails/plugins/acts_as_ordered/README)
