Forum: Ruby on Rails previous|next navigation question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3504745159d79c585e4f7df8df76aa10?d=identicon&s=25 Ingo Weiss (ingoweiss)
on 2006-04-20 08:30
When creating "< previous|next >"-type navigation to browse through
records from a list of search results, what is the most efficient (best
practices-) way to determine what the previous and next records are for
a given record?

Should I store the search results array and the position (index) of the
current record in the session?

Or restore the results array each time (running the same search again)
and then determine the position of the current record inside that array
somehow (How can this be done in Ruby)? Sounds very inefficient to me.

Or even add two instance variables (@prev_record, @next_record) to the
record's model that are populated (with the adjacent records) each time
a search is run, and then store the search results array in the session?

Or ...?

Ingo Weiss
3504745159d79c585e4f7df8df76aa10?d=identicon&s=25 Ingo Weiss (ingoweiss)
on 2006-04-20 08:34
Or subclass the record's model and add @prev/@next to that subclass,
then use it instead of the original record's class for searches?

Ingo Weiss
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2006-04-20 08:37
Ingo Weiss wrote:
> When creating "< previous|next >"-type navigation to browse through
> records from a list of search results, what is the most efficient (best
> practices-) way to determine what the previous and next records are for
> a given record?

What I did was to paginate the model with a per_page value of one.

That way I get a single item on the page and the paginator object
handles the previous/next navigation for me.

Jeff Coleman
This topic is locked and can not be replied to.