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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:19:13+00:00 2026-05-20T08:19:13+00:00

I have a model called note . I currently get notes like this: current_user.notes.order(‘date

  • 0

I have a model called note. I currently get notes like this:

current_user.notes.order('date DESC, created_at DESC').limit(10).reverse!

I now need to do the following:
Take a note_id parameter, and return 10 notes: the one from the parameter and 9 other ones ‘around’ the first.

Ideally, the 9 other notes will be allocated as 5 before the primary note and 4 after (or 4 before & 5 after), based on the ordering in the first line above.

Example:

note note note note primary_note note note note note note

Sometimes this will not be possible. For example if the primary_note is the users second note, it should return this:

note primary_note note note note note note note note note

Or if it’s the latest note, and the user only has 3 notes total, it should return:

note note primary_note

How can I do this? I’m using ruby 1.9.2 & Rails 3.0.1

  • 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-20T08:19:13+00:00Added an answer on May 20, 2026 at 8:19 am

    I don’t think there is a way of obtaining those notes with a single query – you will need at least two. From my (somewhat limited) understanding of SQL, the only way you have to do that is:

    • Obtain the 4 notes that come “before” primary_note
    • Obtain the 4 notes “after” primary_note
    • Joining the two sets together

    Something similar to this (warning: untested code)

    class Note < ActiveRecord::Base
      # on rails 3 I prefer class methods to named scopes.
      # they are equally "chainable" and have simpler syntax
      def self.notes_before(date, limit=4)
        self.where(['date >= ?', date]).order("date DESC, created_at DESC").limit(limit)
      end
    
      def self.notes_after(date, limit=4)
        self.where(['date <= ?', date]).order("created_at ASC, date ASC").limit(limit)
      end
    
      # Instance method
      def notes_around(limit=4)
        (Note.notes_before(self.date, limit) << self) + Note.notes_after(self.date, limit)
      end
    end
    

    Usage:

    n = Note.new(:date => Date.yesterday)
    n.notes_around # returns the 9 notes around n, plus n
    n.notes_around(40) # returns 81 notes
    
    # in addition:
    Note.notes_before(Date.today, 40) # last 40 notes
    Note.notes_after(Date.today, 10).where(:color => 'blue') # chainable
    

    But I could not think of a “chainable class method”. Maybe I don’t know enough SQL.

    Ideally, the 9 other notes will be
    allocated as 5 before the primary note
    and 4 after (or 4 before & 5 after),
    based on the ordering in the first
    line above

    I don’t understand how you determine that you need “4” or “5” on this context. So I’m assuming that you know that.

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

Sidebar

Related Questions

I have a class in my domain model called JobPlan this class is stored
I have an application that manages documents called Notes. Like a blog, Notes can
Let's assume I have a model called product. Let's assume that product has three
I have a simple model called Party with a corresponding table called parties .
I have a model Question with a field called userid , before one ask
In my user model, I have an attribute called nickname and validates as such:
I've got a model in CakePHP that doesn't have a table, called Upload. I've
I have 2 models: notes and tags. class Note < ActiveRecord::Base has_and_belongs_to_many :tags end
I have a model called NoteCategory, which is functioning as a join table between
I have model/controller called notification. I want to make a new url so that

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.