I’m trying to sort a model of mine in a view by a value that’s not stored in my db but is a model method.
My Articles of a score method, that is basically just the article comments + votes_for from vote_fu. In my controller, my articles are assigned like so:
@articles = @topic.articles
@articles.sort! { |article| article.score }
But yet, when viewing my page, the articles seem to be displayed randomly on the page. In my article.rb model, score is defined as:
def score
self.comments.count + self.votes_for
end
Anyway, I have no pagination or anything of the like, it’s a pretty basic app. Just wondering if anyone could give me some pointers on what I’m doing wrong trying to sort this way.
You need to use
.sort_byfor this.