Next/ Previous record

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 :frowning:

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.

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 :frowning:

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

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

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.

I’ve written a plugin to do this.

http://svn.viney.net.nz/things/rails/plugins/acts_as_ordered

-Jonathan

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! :slight_smile:
Sorry for repeating the question - I’ll take a look at the plugin as
well as the previous thread.

Cheers
Mohit.

Mohit S. [email protected] 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

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://www.kreeti.com
Read my blog at: http://cuttingtheredtape.blogspot.com/
,----
| “War is Peace! Freedom is Slavery! Ignorance is Strength!”
| – Orwell, 1984, 1948
`----

Jonathan V. wrote:

I’ve written a plugin to do this.

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.

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.

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 :slight_smile:
Cheers
Mohit.

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.