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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:26:32+00:00 2026-06-18T01:26:32+00:00

I have a series of users who each make numerous posts (and the posts

  • 0

I have a series of users who each make numerous posts (and the posts receive “views”), and I have sorted them in various ways in the past.

For example, here is the 8 most viewed users:

@most_viewed = User.sort_by{|user| user.views}

And then in my model, user.rb file:

def views
    self.posts.sum(:views) || 0
end

This worked fine in the past.

The problem is that my sorting used the sort_by method, which doesn’t play nice with the will_paginate gem because it returns an Array instead of a Relation.

How can I do the following things in Rails 3, Active Record Relation, Lazy Loading style? For example, as named scopes?

  • Users with at least one post
  • Users with the greatest number of total views on their posts
  • Users with the posts with the greatest number of views (not total)
  • Users with the most recent posts
  • Users with the most number of posts
  • 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-06-18T01:26:33+00:00Added an answer on June 18, 2026 at 1:26 am

    You can still paginate an array, but you will have to require ‘will_paginate/array’
    To do this, you could create a file (i.e.: “will_paginate_array_fix.rb”) in your config/initializers directory. Add this as only line in that file:

    require 'will_paginate/array'
    

    restart your server, and you should be able to paginate normal ruby arrays.

    you should use the order method. This will perform the sorting through SQL, which give better performance in the first place 🙂

    User.order("users.views DESC")
    

    or for ascending order

    User.order("users.views ASC")
    

    This will return a relation, so you can add other stuff such as where clauses, scoping, etc..

    User.order("views DESC").where("name like 'r%'")
    

    For an ordered list of users who’s name start with r

    visit http://guides.rubyonrails.org/active_record_querying.html for the official explanation

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

Sidebar

Related Questions

I have a series of complex views being displayed on the screen and I
Greetings Overflow users, I created a series of custom ComboBox controls, that should have
I have a users click a series of checkboxes to identify which areas a
i have a page with a series of checkboxes that authenticated users can change.
I all, I have a series of MYSQL databases with different users and passwords,
I have a series of objects that are associated with specific users, like this:
I have a series of directories on Linux and each directory contains lots of
I have a series of events (with timestamps) that are linked to users: #
I have a series of composite user controls that inherit from a base class
I have a series of PHP page, and I would like to use conditional

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.