Skip to content

Instantly share code, notes, and snippets.

@abitdodgy
Last active December 18, 2015 18:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save abitdodgy/5826969 to your computer and use it in GitHub Desktop.
Save abitdodgy/5826969 to your computer and use it in GitHub Desktop.
ActiveRecord "Hot" algorithm.

A sorting algorithm for sorting by "hotness", roughly based on Reddit's algorithm with a few tweeks.

scope :order_by_hot, ->
  { order("round((votes_count - 1) / POW(DATE_PART('day', Now() - created_at) * 24 + DATE_PART('hour', Now() - created_at) + 2, 1.5)::numeric, 8) DESC, votes_count DESC") }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment