So I should not use Post::all()?
@Eco012390 Well if you do, it’s going to issue a SELECT * FROM articles SQL statement, and if you have a million rows in your table then it’s going to be slow. Especially if you loop through results and do more calculation before rendering your view.
You can either create a “virtual” column as part of your query, or will need to save the “rank” against an article. Otherwise your app’s just going to get slower and slower. Looking at your code, you could query like:
SELECT articles.*, (articles.weight * (articles.followed + articles.interaction) * articles.created_at) AS edge FROM articles ORDER BY edge DESC LIMIT 0, 15
This will give you the first 15 articles, sorted in descending order by the edge value.
You’ll also need to look into indexes to ensure this query is as optimal as possible.