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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:35:48+00:00 2026-05-27T01:35:48+00:00

Ruby 1.9.2 / rails 3.1 / deploy onto heroku –> posgresql Hi, Once a

  • 0

Ruby 1.9.2 / rails 3.1 / deploy onto heroku –> posgresql

Hi, Once a number of rows relating to an object goes over a certain amount, I wish to pull back every nth row instead. It’s simply because the rows are used (in part) to display data for graphing, so once the number of rows returned goes above say 20, it’s good to return every second one, and so forth.

This question seemed to point in the right direction:
ActiveRecord Find – Skipping Records or Getting Every Nth Record

Doing a mod on row number makes sense, but using basically:

@widgetstats = self.widgetstats.find(:all,:conditions => 'MOD(ROW_NUMBER(),3) = 0 ')

doesn’t work, it returns an error:

PGError: ERROR:  window function call requires an OVER clause

And any attempt to solve that with e.g. basing my OVER clause syntax on things I see in the answer on this question:

Row numbering in PostgreSQL

ends in syntax errors and I can’t get a result.

Am I missing a more obvious way of efficiently returning every nth task or if I’m on the right track any pointers on the way to go? Obviously returning all the data and fixing it in rails afterwards is possible, but terribly inefficient.

Thank you!

  • 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-27T01:35:49+00:00Added an answer on May 27, 2026 at 1:35 am

    I think you are looking for a query like this one:

    SELECT * FROM (SELECT widgetstats.*, row_number() OVER () AS rownum FROM widgetstats ORDER BY id) stats WHERE mod(rownum,3) = 0
    

    This is difficult to build using ActiveRecord, so you might be forced to do something like:

    @widgetstats = self.widgetstats.find_by_sql(
      %{
        SELECT * FROM
        (
          SELECT widgetstats.*, row_number() OVER () AS rownum FROM widgetstats ORDER BY id
        ) AS stats
        WHERE mod(rownum,3) = 0
      }
    )
    

    You’ll obviously want to change the ordering used and add any WHERE clauses or other modifications to suit your needs.

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

Sidebar

Related Questions

Once I've finished my Ruby On Rails app, I want to deploy it on
This is not a ruby/rails project deploy. I have the following situation and I
I am trying to deploy my Ruby on Rails application on Media Temple. The
I have to to deploy a Ruby on Rails application to an tomcat application
I'm looking at ways to deploy a Ruby on Rails app (running on JRuby)
We're using EngineYard Cloud to deploy our Ruby on Rails application. We are running
I am using Capistrano to deploy a ruby on rails project. I am attempting
I'm writing a Ruby on Rails app that normally runs on Heroku or a
Can I deploy a Ruby on Rails webapp to an IIS webserver? Is there
I am using capistrano to deploy my Ruby on Rails project. I am on

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.