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 7668821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:27:16+00:00 2026-05-31T15:27:16+00:00

I have scopes defined in an include module like this: module ActsAsAdjacent def self.included(base)

  • 0

I have scopes defined in an include module like this:

module ActsAsAdjacent

  def self.included(base)

    base.send(:scope, :next, lambda {|id| {:conditions => ["id > ?",id],
       :order => "id ASC", :limit => 1}}) 

    base.send(:scope, :previous, lambda {|id| {:conditions => ["id < ?",id],
       :order => "id DESC", :limit => 1}})     
  end

, the idea being that you can mixin next, previous capability into any ActiveRecord::Base subclass. (The above is a variant of some examples floating around).

The obvious draw back to the above though is that it is assuming a specific column, ‘id’, whereas that column may not exist in some instances, or there may be a different sort order being used.

Is there a way to do next, previous based on whatever the current sort order is, or how ever the records are layed out in the db.

So for example in the following case,

class User < ActiveRecord::Base

  default_scope :order => 'users.name ASC'

  include ActsAsAdjacent

ActsAsAdjacent would automatically use that sort order to return next, previous.


I guess id will always exist, but it may not be sorted on that.

  • 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-31T15:27:17+00:00Added an answer on May 31, 2026 at 3:27 pm

    I think that’s a misuse of scopes. How about this?

    module ActsAsAdjacent
      def next_by(column)
        self.class.where("#{column} > ?", send(column)).order(column).first
      end
    
      # .. and the obvious inverse for previous_by
    end
    
    # Call it like this:
    some_user.next_by(:id)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have defined the following two scopes and both working fine: scope :regular_friends, lambda
we have a cache which I would like to put some transaction scopes around
I have a basic C++ class .The header looks like this: #pragma once class
Is it possible to have scoped macros using custom defined macros through boost wave?
In my model I have three complex scopes returning the Discount object arrays. Each
Is it possible to have a hierarchy of transaction scopes? If the outer transaction
I have a group of CFC's that I access from two seperate Applicaiton scopes.
If I have a scope with a lambda and it takes an argument, depending
I have the following scope, which I know is not optimal: scope :event_stream_for, lambda{
What is the scope of variables in javascript? Do they have the same scope

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.