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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:54:01+00:00 2026-06-12T05:54:01+00:00

How can I set up the default_scope in my blogging application so that the

  • 0

How can I set up the default_scope in my blogging application so that the index orders the entries by an algorithm defined in the model?

If I were to use a HackerNews-like formula for the ranking algorithm as shown below, how can I define it in my model?

total_score = (votes_gained - 1) / (age_in_hours + 2)^1.5

The votes_gained variable relies on the Active_Record_Reputation_System, and is written as the following in my views:

votes_gained = @post.reputation_value_for(:votes).to_i

Finally, age_in_hours is pretty straight forward

age_in_hours = (Time.now - @post.created_at)/1.hour

How can I use these figures to order my blog posts index? I’ve been trying to figure out how to define total_score correctly in the model so that I can add it into the default scope as default_scope order("total_score DESC") or something similar. Direct substitution has not worked, and I’m not sure of how to “rephrase” each part of the formula.

How exactly should I define total_score? Thanks much for your insight!

  • 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-12T05:54:02+00:00Added an answer on June 12, 2026 at 5:54 am

    Seeing as how you can’t rely on active record to translate the formula into SQL, you have to write it yourself. The only potential concern here is that this is not a database-independent solution.

    Since you are using Postgres, you can define your scope as (I haven’t tested this yet, so let me know whether it works):

    AGE_IN_HOURS = "(#{Time.now.tv_sec} - EXTRACT (EPOCH FROM posts.created_at))/3600"
    TOTAL_SCORE = "(rs_reputations.value - 1)/((#{AGE_IN_HOURS}) + 2)^1.5"
    
    default_scope joins("INNER JOIN rs_reputations ON rs_reputations.target_id = posts.id").where("rs_reputations.target_type = 'Post'").order(TOTAL_SCORE)
    

    EDIT: Actually this won’t work because, as it stands, Time.now is calculated one time (when the model loads), but it needs to be recalculated each time records are pulled. Use

    default_scope lambda { order_by_score }
    
    def self.order_by_score
    
        age_in_hours = "(#{Time.now.tv_sec} - EXTRACT (EPOCH FROM posts.created_at))/3600"
        total_score = "(rs_reputations.value - 1)/((#{age_in_hours}) + 2)^1.5"
    
        joins("INNER JOIN rs_reputations ON rs_reputations.target_id = posts.id").where("rs_reputations.target_type = 'Post'").order(TOTAL_SCORE)
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way that can I set default value as Empty.string in Model.
So I've got a class that I'd like to have it just set defaults
I like to use the Find in Path function CTRL+SHIFT+F , that searches for
how can i set default value in sonata admin bundle the data option is
How can i set default home page in asp web.config file. i have already
I'm searching for a strategy with which I can set the default sortorder of
for String and Object type, I can set the default parameter to null to
How can you set a default input value in a .net console app? Here
How can I set a default value such as don't have any information instead
How can we set element to some default date in mysql ? In oracle

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.