Ranking system for popular + recent posts like either Hacker News or Reddit?

In my application that I am working on which is a social networking app, I already have Post model that has fields:

create_table "posts", force: true do |t|
  t.integer "user_id"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.integer  "comments_count", default: 0
  t.integer  "likers_count",   default: 0

I found Hacker News’ algorithm:

Score = (P-1) / (T+2)^G

P = points of an item (and -1 is to negate submitters vote)
T = time since submission (in hours)
G = Gravity, defaults to 1.8 in news.arc

How do you implement this algorithm in Rails app for practical way? Because if we calculate in SQL order by clause, it will scan entire table on every request? How do you implement similar functionality in your application?