Skip to content
Advertisement

Building a trending algorithm based on post count and frequency

Say I have a Board model. Boards have many Posts. I simply want to find the boards that have the highest post count within the span of (x) days. Below is my extremely naive approach to this. With the code provided I get the error:


Please let me know if there’s a better way to do this in addition the error I’m receiving.

Update: So I managed to come up with a working scope but not 100% sure if it’s the most optimal. Your thoughts would be appreciated:

Advertisement

Answer

Update:

Try this, you will need to join it and group them by board_id

Explanation:

  • We joined (inner join) the tables so by default you get only boards which has at least one post associated with it
  • we ordered them based on posts count
  • we grouped them based on boards.id
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement