Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 755085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:05:19+00:00 2026-05-14T15:05:19+00:00

I have a model for which I want to retrieve the next record(s) and

  • 0

I have a model for which I want to retrieve the next record(s) and previous record(s). I want to do this via a named_scope on the model, and also pass in as an argument the X number of next/previous records to return.

For example, let’s say I have 5 records:

  • Record1
  • Record2
  • Record3
  • Record4
  • Record5

I want to be able to call Model.previous or Model.previous(1) to return Record2. Similarly, I want to be able to call Model.next or Model.next(1) to return Record4. As another example I want to be able to call Model.previous(2) to return Record3. I think you get the idea.

How can I accomplish this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:05:19+00:00Added an answer on May 14, 2026 at 3:05 pm

    To implement something like ‘Model.previous’, the class itself would have to have a ‘current’ state. That would make sense if the ‘current’ record (perhaps in a publishing scheduling system?) was Record3 in your example, but your example doesn’t suggest this.

    If you want to take an instance of a model and get the next or previous record, the following is a simple example:

    class Page < ActiveRecord::Base
      def previous(offset = 0)    
        self.class.first(:conditions => ['id < ?', self.id], :limit => 1, :offset => offset, :order => "id DESC")
      end
    
      def next(offset = 0)
        self.class.first(:conditions => ['id > ?', self.id], :limit => 1, :offset => offset, :order => "id ASC")
      end
    end
    

    If so you could do something like:

    @page = Page.find(4)
    @page.previous
    

    Also working would be:

    @page.previous(1)
    @page.next
    @page.next(1)
    

    Obviously, this assumes that the idea of ‘next’ and ‘previous’ is by the ‘id’ field, which probably wouldn’t extend very well over the life of an application.

    If you did want to use this on the class, you could perhaps extend this into a named scope that takes the ‘current’ record as an argument. Something like this:

    named_scope :previous, lambda { |current, offset| { :conditions => ['id < ?', current], :limit => 1, :offset => offset, :order => "id DESC" }}
    

    Which means you could call:

    Page.previous(4,1)
    

    Where ‘4’ is the id of the record you want to start on, and 1 is the number you want to navigate back.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have django model which consist of parent child relationship filed I want to
I am making a model in which i have a FileField . I want
I want a Django model to have a date field in which the year,
I have a model which contains lists of other models, which also contains lists
i have this method on which i retrieve first a Varien_Data_Collection and then I
I have a Web Service ( model-layer ) which communicates with Database. This module
I have a variable length collection of address models which I want to edit.
I have model Foo which has field bar. The bar field should be unique,
I have a Model which has some constants defined, like below: class Order(models.Model): WAITING
I have a model which defines a property with either markdown or html content.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.