# Next/ Previous record

**URL:** <https://www.ruby-forum.com/t/next-previous-record/102040>\
**Category:** Rails\
**Created:** [August 9, 2006, 2:19pm UTC](https://www.ruby-forum.com/t/next-previous-record/102040 "2006-08-09T14:19:34Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [August 9, 2006, 2:19pm UTC](https://www.ruby-forum.com/t/next-previous-record/102040/1 "2006-08-09T14:19:34Z")

</div>

Hi, Quick question (which means that I think there should be an easy  
answer)…

When I am in the “show” view, I’d like to add 2 links, one for previous  
and one for next record so that the user can go on to the next “result”  
without having to go back to the ‘list’ of results. However, I can’t  
find my way about this problem :-S

I’m not sure where I should look… in my brief search through the  
manuals, I was unable to discover much ☹

I’d imagine one way would be to use pagination with 1 record per page,  
but surely there’s another way given that the records have already been  
loaded from the database?  
Any help?  
Thanks  
Mohit.

---

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [August 9, 2006, 5:02pm UTC](https://www.ruby-forum.com/t/next-previous-record/102040/2 "2006-08-09T17:02:30Z")

</div>

Mohit S. wrote:

> Hi, Quick question (which means that I think there should be an easy  
> answer)…
> 
> When I am in the “show” view, I’d like to add 2 links, one for previous  
> and one for next record so that the user can go on to the next “result”  
> without having to go back to the ‘list’ of results. However, I can’t  
> find my way about this problem :-S
> 
> I’m not sure where I should look… in my brief search through the  
> manuals, I was unable to discover much ☹
> 
> I’d imagine one way would be to use pagination with 1 record per page,  
> but surely there’s another way given that the records have already been  
> loaded from the database?  
> Any help?  
> Thanks  
> Mohit.

This question was asked last week and I suggested acts\_as\_ordered. If  
you can’t find the plugin then look through the archives for last week  
with similar title.

Regards,

Michael

---

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [August 9, 2006, 7:50pm UTC](https://www.ruby-forum.com/t/next-previous-record/102040/3 "2006-08-09T19:50:44Z")

</div>

Michael M. wrote:

> This question was asked last week and I suggested acts\_as\_ordered. If  
> you can’t find the plugin then look through the archives for last week  
> with similar title.
> 
> Regards,
> 
> Michael

And thank you, Michael, for pointing this out to me last week. I works.

However, navigating to the Next or Previous record is slow – about 45  
seconds. I’m wondering what optimizations can be done to increase  
performance.

gk

---

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [August 15, 2006, 2:09pm UTC](https://www.ruby-forum.com/t/next-previous-record/102040/4 "2006-08-15T14:09:10Z")

</div>

Really? Using acts\_as\_ordered?

There must be something strange happening. It will be a tiny bit  
slower than just viewing a record normally, because it may use two or  
three queries rather than just one, but no way should it be 45  
seconds. I use the plugin in a few applications and would have noticed  
an issue as big as that!

-Jonathan.

---

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [August 15, 2006, 2:09pm UTC](https://www.ruby-forum.com/t/next-previous-record/102040/5 "2006-08-15T14:09:23Z")

</div>

I’ve written a plugin to do this.

[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:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [August 15, 2006, 2:10pm UTC](https://www.ruby-forum.com/t/next-previous-record/102040/6 "2006-08-15T14:10:38Z")

</div>

Michael M. wrote:

> > I’m not sure where I should look… in my brief search through the  
> > This question was asked last week and I suggested acts\_as\_ordered. If  
> > you can’t find the plugin then look through the archives for last week  
> > with similar title.
> 
> Regards,
> 
> Michael

Oops! I must have missed that reply in the barrage of emails that this  
list brings with it! 🙂  
Sorry for repeating the question - I’ll take a look at the plugin as  
well as the previous thread.

Cheers  
Mohit.

---

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [August 15, 2006, 2:13pm UTC](https://www.ruby-forum.com/t/next-previous-record/102040/7 "2006-08-15T14:13:53Z")

</div>

Mohit S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> Hi, Quick question (which means that I think there should be an easy  
> answer)…
> 
> When I am in the “show” view, I’d like to add 2 links, one for  
> previous and one for next record so that the user can go on to the  
> next “result” without having to go back to the ‘list’ of results.  
> However, I can’t find my way about this problem :-S

this is very simple to do, look here

[http://cuttingtheredtape.blogspot.com/2006/08/rails-model-next-and-previous-objects.html](http://cuttingtheredtape.blogspot.com/2006/08/rails-model-next-and-previous-objects.html)

also, use ‘with\_scope’, if you want some sort of filtering, that will  
ensure  
the filtering is applied to the code in the link also.

–  
Surendra S.  
[http://ssinghi.kreeti.com](http://ssinghi.kreeti.com), [http://www.kreeti.com](http://www.kreeti.com)  
Read my blog at: [http://cuttingtheredtape.blogspot.com/](http://cuttingtheredtape.blogspot.com/)  
,----  
| “War is Peace! Freedom is Slavery! Ignorance is Strength!”  
| – Orwell, 1984, 1948  
`----

---

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [August 15, 2006, 2:18pm UTC](https://www.ruby-forum.com/t/next-previous-record/102040/8 "2006-08-15T14:18:29Z")

</div>

Jonathan V. wrote:

> I’ve written a plugin to do this.
> 
> [http://svn.viney.net.nz/things/rails/plugins/acts\_as\_ordered](http://svn.viney.net.nz/things/rails/plugins/acts_as_ordered)
> 
> -Jonathan

Hi Jonathan,

Just downloaded and installed the plugin. According to what I read, it  
seems that it does some of what to do. I guess there’s one main  
question. According to the documentation (readme), acts\_as\_ordered goes  
into the model. This means that the order sequence is stored in the  
model.

Will this work with searches? Also, I allow my users to sort the list  
using any of the main fields (name, date, number - descending &  
ascending for each) - just wondering how I could use it with these  
constraints?

Perhaps it’s because I don’t understand this in the documentation:

You can use this to get the next/previous model that matches some  
condition:  
class Person \< ActiveRecord::Base  
acts\_as\_ordered :condition =\> Proc.new { |p| p.active? }  
end

Thanks  
Mohit.

---

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [August 15, 2006, 2:10pm UTC](https://www.ruby-forum.com/t/next-previous-record/102040/9 "2006-08-15T14:10:43Z")

</div>

Is it possible you have a LOT of records in the table in question?

In any case, I would think the column the ordering is done on would  
absolutely need to be indexed for optimum performance.

---

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [August 15, 2006, 2:22pm UTC](https://www.ruby-forum.com/t/next-previous-record/102040/10 "2006-08-15T14:22:12Z")

</div>

Thanks Jonathan (both for writing the plugin and also pointing me to  
it)… although I’m a bit surprised that there’s no in-built way to deal  
with this issue…

OK, I guess it’s time to start looking at using plugins then 🙂  
Cheers  
Mohit.

---

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [September 4, 2006, 2:45pm UTC](https://www.ruby-forum.com/t/next-previous-record/102040/11 "2006-09-04T14:45:29Z")

</div>

That is a use case I had not considered. At the moment, the order is  
set once in the model definition and cannot be easily changed in the  
way you require.

There are two ways to determine what records are used when the next or  
previous one is requested.

:scope - A piece of sql that is included when the list of ordered  
ids are found. So if you had a has\_many relationship between Customer  
and Account, and wanted a next/previous function on a customer’s  
accounts, you would use acts\_as\_order :scope =\> :customer\_id, or  
:scope =\> ‘customer\_id = #{customer\_id}’, to restrict the records to  
ones with the same customer id.

:condition - A symbol, or proc, that should return a boolean value.  
Use this when you need more complicated conditions to determine  
whether or not a model object should be returned from a next/previous  
call. The model is instantiated, but only returned if the :condition  
returns true, if not it will call next/previous until the condition is  
satisfied or there are no more models left. Use this when the  
restictions can’t be easily specified in sql with :scope.

-Jonathan.
