How to fetch the previous or next record

Hello,
Is there some built-in facility for fetching the Previous or Next record
from a table? Assume the command find_by_name has been issued, and the
variable @name contains the name “John D.”. I’m looking for something
like find_next(@name) and find_previous(@name). What’s a good way to
pursue if one has to roll their own methods?

Thanks for the help,
gk

Gene K. wrote:

Hello,
Is there some built-in facility for fetching the Previous or Next record
from a table? Assume the command find_by_name has been issued, and the
variable @name contains the name “John D.”. I’m looking for something
like find_next(@name) and find_previous(@name). What’s a good way to
pursue if one has to roll their own methods?

Thanks for the help,
gk

Gene,

I have done something very similar by using pagination and setting
per_page = 1 . Then, when I reference the first item, I use @model[0]
to get the details of the item. For example, if my model is leads, then
I use @lead = @leads[0]

This allows me to use a detail or edit view instead of a list view. Not
really sure if this is the best approach or not, but it is the way I do
it.

Alternatively, you could build the SQL and use an offset. Just keep the
offset stored somewhere so you can decrement or increment as necessary.

I’m sure others may have a better answer but I at least wanted to
contribute an answer that works for me.

Regards,

Michael

Hi,
Thank you for your reply.

The Previous and Next functions are to be built into the Show view. The
Show view when triggered is supplied the id of the record to show, as
per the default Show behavior:

def show
@eng_lemma = EngLemma.find(params[:id])
end

While viewing a record in Show, this is where the Previous and Next
functions are needed, as well as a Find function. Since Show retrieves a
list of one record only, wouldn’t incrementing or decrementing the
@model[i] index yield nothing?

I’m just not understanding it.
Thanks again,
gk

Michael M. wrote:

Gene K. wrote:

Hello,
Is there some built-in facility for fetching the Previous or Next record
from a table? Assume the command find_by_name has been issued, and the
variable @name contains the name “John D.”. I’m looking for something
like find_next(@name) and find_previous(@name). What’s a good way to
pursue if one has to roll their own methods?

Thanks for the help,
gk

Gene,

I have done something very similar by using pagination and setting
per_page = 1 . Then, when I reference the first item, I use @model[0]
to get the details of the item. For example, if my model is leads, then
I use @lead = @leads[0]

This allows me to use a detail or edit view instead of a list view. Not
really sure if this is the best approach or not, but it is the way I do
it.

Alternatively, you could build the SQL and use an offset. Just keep the
offset stored somewhere so you can decrement or increment as necessary.

I’m sure others may have a better answer but I at least wanted to
contribute an answer that works for me.

Regards,

Michael

Gene,

My solution won’t work for you I don’t think! Now that you explained it
further, my sample is invalid. You want that functionality from ANY
show view - whereas I needed to show a list of records in a show/edit
manner and navigate between them. In the solution I provided, you
wouldn’t be able to go to a list, select an item, go to show, and then
from show get the next/previous record.

Maybe someone else will be able to come up with a solution for you.

Sorry about that…

Michael

Michael M. wrote:

I think what you want is something like acts_as_ordered:

Wow that’s a wicked plugin. Thanks for sharing. :slight_smile:

  • Daniel

I think what you want is something like acts_as_ordered:

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

Regards,

Michael

Michael M. wrote:

I think what you want is something like acts_as_ordered:

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

Regards,

Michael

Looks like what I need! Thanks for the lead.

gk

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

if you don’t want/need a plugin you can send the id of the current
record to your next/prev methods in the controller and then do the
following:

#For Next Record
@foo = Foo.find(:first, :conditions => [“id > ?”, params[:id]])

#For Prev Record
@foo = Foo.find(:first, :conditions => [“id < ?”, params[:id]])

If you reach the end or start of the record list you can do a redirect
or something more creative depending on your needs. This will work with
dates, string and integer fields as well.

David H. wrote:

#For Prev Record
@foo = Foo.find(:first, :conditions => [“id < ?”, params[:id]])

Ooops - the above should include :order => ‘id DESC’ at the end or else
it returns the first record in the table.

On 2/12/08, David H. [email protected] wrote:

David H. wrote:

#For Prev Record
@foo = Foo.find(:first, :conditions => [“id < ?”, params[:id]])

Ooops - the above should include :order => ‘id DESC’ at the end or else
it returns the first record in the table.

And the next case should include :order => "id’

There’s really no guarantee that an unordered find will sort by primary
key.


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/