jckly
jckly

Reputation: 851

Where to put score functionality?

I am implementing a score system to rank trending in a rails application. To score the items I'm using a basic number of likes x age.

@post.score = @post.likers(User).length * age

I have a field in the posts database called score. My question is where do I call the above code so that the score is constantly getting updated as it gets older or when someone new likes the post.

Thanks for any help.

Upvotes: 0

Views: 86

Answers (2)

Yule
Yule

Reputation: 9774

As it's an ever changing value, and not really a static field, I'd suggest consider putting this in a method, not in the database. Unless for performance reasons that is needed. So, in your Post model just have:

def score
 #score algorithm here
end

This way, whenever you call post.score, it will be calculated at that time and shown.

Alternatively if age is a daily value, you could use some kind of scheduled task (cron/whenever) to update this on a daily basis.

Upvotes: 1

Justus Eapen
Justus Eapen

Reputation: 1159

The code you posted should work. Just make sure you call @post.save on the next line. Can we see your codebase?

Upvotes: 0

Related Questions