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

  • Home
  • SEARCH
  • 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 4045166
In Process

The Archive Base Latest Questions

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

BACKGROUND: I have a set of Posts that can be voted on. I’d like

  • 0

BACKGROUND: I have a set of Posts that can be voted on. I’d like to sort Posts according to their “vote score” which is determined by the following equation:

( (@post.votes.count) / ( (Time.now – @post.created_at) ** 1 ) )

I am currently defining the vote score as such:

  def vote_score(x)
   ( (x.votes.count) / ( (Time.now - x.created_at) ** 1 ) )
  end

And sorting them as such:

@posts = @posts.sort! { |a,b| vote_score((b) <=> vote_score((a) }

OBJECTIVE: This method takes a tremendous toll on my apps load times. Is there a better, more efficient way to accomplish this kind of sorting?

  • 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-20T13:23:13+00:00Added an answer on May 20, 2026 at 1:23 pm

    If you are using MySQL you can do the entire thing using a query:

    SELECT   posts.id,
             (COUNT(votes.id)/(TIME_TO_SEC(NOW()) - TIME_TO_SEC(posts.created_at))) as score
    FROM     posts INNER JOIN votes ON votes.post_id = posts.id
    GROUP BY posts.id
    ORDER BY score DESC
    

    Or:

    class Post
      scope :with_score, select('posts.*')
        .select('(COUNT(votes.id)/(TIME_TO_SEC(NOW()) - TIME_TO_SEC(posts.created_at))) as score')
        .joins(:votes)
        .group('posts.id')
        .order('score DESC')
    end
    

    Which would make your entire query:

    @posts = Post.with_score.all
    

    P.S: You can then modify your Post class to use the SQL version of score if it is present. You can also make the score function cached in an instance so you don’t have to re-calculate it every time you ask for a post’s score:

    class Post
      def score
        @score ||= self[:score] || (votes.count/(Time.now.utc - x.created_at.utc)
      end
    end
    

    P.S: The SQLLite3 equivalent is:

    strftime('%s','now') - strftime('%s',posts.created_at)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple fabric.canvas element which has a background and I have set
I have set a background on the data-role=page element like so <div data-role=page style=background:
I have one AlertDialog which is working fine.I have set some background images to
I have a page where the background colour can be set dynamically. In some
Background I have an array of objects (Users) defined and set as follows: //
let's say I have this set of HTML-markup and CSS #CSS .inputhelp_text { background:
In a QGraphicsScene , I have a background set with a few QGraphicsItem s
I have a sprite image set as the background of an element on my
I have a page where the body is set to a background colour. I
Well i have a transparent div or the background is set to transparent :)

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.