# ActiveRecord Previous/Next Record

**URL:** <https://www.ruby-forum.com/t/activerecord-previous-next-record/103062>\
**Category:** Rails\
**Created:** [May 1, 2007, 10:57am UTC](https://www.ruby-forum.com/t/activerecord-previous-next-record/103062 "2007-05-01T10:57:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 1, 2007, 10:57am UTC](https://www.ruby-forum.com/t/activerecord-previous-next-record/103062/1 "2007-05-01T10:57:42Z")

</div>

Does ActiveRecord offer a simple solution to get previous/next records  
relative to a certain record? For instance, say I have this table:

PEOPLE  
id | name  
1 | john  
2 | mary  
3 | bob

Using p = Person.find(2), is it possible to get the previous (john)  
and next (bob) records using perhaps something like p.previous and  
p.next? If not, how can I do this without resorting to ugly queries?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 1, 2007, 11:45am UTC](https://www.ruby-forum.com/t/activerecord-previous-next-record/103062/2 "2007-05-01T11:45:44Z")

</div>

Try my acts\_as\_ordered plugin:

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

-Jonathan.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 1, 2007, 1:00pm UTC](https://www.ruby-forum.com/t/activerecord-previous-next-record/103062/3 "2007-05-01T13:00:50Z")

</div>

Exactly what I needed, thanks!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 1, 2007, 2:13pm UTC](https://www.ruby-forum.com/t/activerecord-previous-next-record/103062/4 "2007-05-01T14:13:54Z")

</div>

Toulax,

> Using p = Person.find(2), is it possible to get the previous (john)  
> and next (bob) records using perhaps something like p.previous and  
> p.next? If not, how can I do this without resorting to ugly  
> queries?

If you

- only need next()
- use MySql (optional)

simply add this in environment.rb :

class ActiveRecord::Base  
def next  
self.class.find(:first, :conditions =\> [‘id \> ?’, self.id])  
end  
end

For non-MySql db, you might have to add

```
 :order => 'id asc'

```

to the query.

(see:  
[Buckblog: Faking cursors in ActiveRecord](http://weblog.jamisbuck.org/2007/4/6/faking-cursors-in-activerecord))

Alain R.  
[http://blog.ravet.com](http://blog.ravet.com)
