Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How would you create weight related to time? #33

Open
dfischer opened this issue Jan 17, 2012 · 5 comments
Open

How would you create weight related to time? #33

dfischer opened this issue Jan 17, 2012 · 5 comments

Comments

@dfischer
Copy link

Hi,

I have interest in adding something that adds more weight the newer the submission is. Otherwise only old entries will be at the top. Does anyone know how to do this?

@bouchard
Copy link
Owner

Hello,

As the code to calculate weight is in Ruby rather than in the DB, this will be inherently slow for large DB tables. With that caveat, the formula we currently have for weighting:

karma = sum(vote * weight), where weight is a fraction from [0,1].

If you want to weight newer items, then you'll want to change that formula to:

karma = sum(vote * weight * vote[time_since_updated_at])

Another hint I'll give you are that these two lines of code are critical for what you want to achieve:

https://github.com/brady8/thumbs_up/blob/master/lib/has_karma.rb#L16
https://github.com/brady8/thumbs_up/blob/master/lib/has_karma.rb#L36

@dfischer
Copy link
Author

How do you sort by karma?

E.g... View the hottest "Questions" sorted by karma value? A question with 5 points yesterday would be below a vote with 5 points today, even possibly if that question had 6 points yesterday it'd still be below the 5 point from today...

@bouchard
Copy link
Owner

That would be a useful method to add to thumbs_up! I would encourage you to look at the source (most of it is quite straightforward), and I'll be happy to take a pull request from you if you come up with something that works. Sorry that I don't have time to code it up myself.

@dfischer
Copy link
Author

I wish my back-end skills were up to chops. My speciality is front-end development. I'll give it a try though...

@dleatham
Copy link

dleatham commented Apr 4, 2012

If you look at this at a slightly higher/abstract level, sorting by karma would be sorting based on a calc based on database query. I had a similar challenge and asked the following question on SO:

http://stackoverflow.com/questions/9847855/rails-3-how-can-you-sort-an-ar-query-result-by-doing-math-on-attributes

When I saw the query that DGM provided in that answer it reminded me that a while back I had also had wanted to sort by karma. Applying the last part of DGM's answer, a karma sort on a votable Post model might be something like:

ten_highest_karma = Post.select('*, sum(vote*weight) as karma').order('karma DESC').limit(10)

I looked into this, but it was quickly obvious that I don't have the technical chops to figure out how to implement the 'sum(vote*weight)' calculation in a DB call, but hopefully someone else might.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants