Ruby on Rails implementation of a ranking system using PostgreSQL
This article will show how easily you can add ranking functionality to your project. We will accomplish it, with the use of ntile() window function. I will be using Ruby on Rails for my application, with PostgreSQL database.

Hello, this article will show, how easily you can add ranking functionality to your project. We will accomplish it, with the use of ntile()
window function. I will be using Ruby on Rails for my application, with PostgreSQL database.
We will need:
A model with integer attribute, by which we want to make the ranking (in my case, it’s
visit_count
). Here is an exemplary migration for Post model:tsx
Controller
tsx
Route for resource
tsx
View for our index action and partial for post
tsx
tsx
Here I won’t implement increment functionality for visit_count
, because it's not the case of this article. We will just generate data by ourselves. For example by running this in Rails console:
tsx
Instead of adding method to Post model, I will just create Query Object called PostWithPopularityQuery
, under app/query_objects/
, to keep the model slim.
tsx
You can see constant POPULARITY_RANGES
. This will be the number of 'levels' of popularity/ranking you want. Let's say it's 5. The ntile()
window function will then apply value in range 1-5 to each record. In other words, it will divide it in n-tiles
.
About the public static .call
method itself: first, we call method .find_by_sql
on model (in this case it's Post), and we just open quotes and write SQL.
What about the SQL? We specify which columns we want to retrieve, then we use the ntile(?)
with OVER
- this will be the criteria for ranking, visit_count exactly. We also have to use order by
, it is required for using ntile. There is also something called popularity
in here. I named it this way, but you can name it as you want. By this name, you will retrieve it’s ranking value, by calling it on instance of record (in my case, it’s post.popularity
)
And now, how do we know what popularity value (between 1 and 5) does each Post have? Simply call .popularity
on Post
instance retrieved by Query Object method.
How our controller will look like now? Simple:
tsx
With templates specified earlier, we see something like this when we visit our Posts#index

Of course you might want something prettier than plain number. With a little effort, you can change it to something cool, for example stars. I decided to add gem font-awesome-rails
for this article demo. With one small change in our partial file _post.html.erb
, like this:
tsx
I've achieved result like this:

Pros of this approach:
Clean and tidy
Fast-working (SQL window function is very efficient)
Quick to add
Little code
Cons: You tell me. Are there any?
Let’s Create a Great Website Together
We'll shape your web platform the way you win it!
More posts in this category
February 05, 2025 • 10 min read
READ MORELearn more about api first cmsAPI-first CMS: What Options You Have (Web Dev Agency's Take)
According to the 2024 State of the API Report by Postman, 74% of organizations are adopting API-first strategies. This statistic isn’t just impressive—it signals a major shift in how businesses operate. While API-first approaches have long been a staple in software development, they're now reshaping content management as well. More and more companies are realizing that traditional CMS platforms can't keep up with the demand for flexibility, speed, and seamless integrations.
January 23, 2025 • 15 min read
READ MORELearn more about best cms for saas top cloud based solutionsBest CMS for SaaS: Top Cloud-Based Solutions
Choosing the right Content Management System (CMS) is a critical decision for your SaaS business. Your unique needs require solutions that are not only flexible, scalable, and user-friendly but also tailored to meet the demands of a fast-paced, customer-focused industry. A CMS should simplify your workflows and help you deliver personalized, high-quality digital experiences.
December 12, 2024 • 10 min read
READ MORELearn more about headless cms for vueWe Picked the Best (Headless) CMS for Vue
Picture a digital experience where content effortlessly flows across platforms, development is agile, and performance is unmatched. By combining the power of Vue.js, a progressive JavaScript framework, with a modern headless CMS, you can achieve all this and more.