Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9133965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:34:45+00:00 2026-06-17T08:34:45+00:00

So I have a social-bookmarking-esque setup here. 4 tables: posts, users, comments, and votes.

  • 0

So I have a social-bookmarking-esque setup here. 4 tables: posts, users, comments, and votes. The homepage simply points to the posts model’s index page and runs a loop over the Post.all array and displays some basic info: title, upvotes – downvotes (I call ti the post’s “score”).

Now what I need to do it order these posts in (i’m guessing) the model by their score divided by their relative creation time (how many seconds since page load was this post created?). Rails gives me a created_at column in my posts object and I have a table of votes which looks like this:

Posts model

class Post < ActiveRecord::Base
  attr_accessible  :id, :title, :url, :user_id, :comment_count, :agreement
  has_many :votes
  has_many :comments
  belongs_to :user
end

Votes model

class Vote < ActiveRecord::Base
  attr_accessible :direction, :post_id, :vote_type, :user_id
  belongs_to :user
  belongs_to :post
  belongs_to :comment
end

IDs in the post model correspond with post_id’s in the vote model.

:direction is a boolean. 0 associated with up voting and 1 associated with downvoting. Post_ID is the ID of the post the vote was cast on.

How should I order the posts so that their position on the homepage is determined by both their creation time AND their up votes – down votes?

def index
    @posts = Post.join(:votes).order(<???>)
end

ETA: @shweta is on the right track with joining the posts and votes tables. The select portion of his answer does not work, however.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:34:46+00:00Added an answer on June 17, 2026 at 8:34 am

    Disclaimer: I don’t know ruby or rails, only SQL.

    shweta’s answer seems really close so I will attempt to finish it with some SQL hack and slash:

    order by upvotes and then created_at

    @posts = Post.joins(:votes).select("posts.id,sum(if(direction = 0, 1, -1)) as score,posts.created_at,OTHER_ATTRS_YOU_NEED").group("posts.id").order("score DESC,posts.created_at")
    

    Also, to include posts with zero votes you will need to do a left join:

    @posts = Post.joins("LEFT JOIN votes ON posts.id = votes.post_id").select("posts.id,sum(if(direction = 0, 1, -1)) as score,posts.created_at,OTHER_ATTRS_YOU_NEED").group("posts.id").order("score DESC,posts.created_at")
    

    Update: to fix the ordering for un-voted posts:

    @posts = Post.joins("LEFT JOIN votes ON posts.id = votes.post_id").select("posts.id,sum(if(direction = 0, 1, if(direction is null, 0, -1))) as score,posts.created_at,OTHER_ATTRS_YOU_NEED").group("posts.id").order("score DESC,posts.created_at")
    

    Update 2: improved ranking

    If you exclude older items you might end up with an empty list (eg on quiet days), which is probably not what you want. You might be better off adding another column for ranking, something like this:

    @posts = Post.joins("LEFT JOIN votes ON posts.id = votes.post_id").select(
        "posts.id," +
        "sum(if(direction = 0, 1, if(direction is null, 0, -1))) as score," +
        "(sum(if(direction = 0, 1, if(direction is null, 0, -1))) * " +
        " if(unix_timestamp() - unix_timestamp(posts.created_at) < 7200, 3, 1)) as rank," +
        "posts.created_at,OTHER_ATTRS_YOU_NEED"
    ).group("posts.id").order("rank DESC,posts.created_at")
    

    For ranking purposes, this triples the score of posts less than 2 hours old.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a social network-esque site with a nice User model that seems to
I have a simple social network graph db model. Users can follow other users
I have multiple fields for social networking websites where users can enter their username
Right now I have this social media app, where I'm implementing ratings on users
OK I have a social network, around 50,000 users so far and there is
I have a social network that allows users to choose fonts for their profiles.
I have a social network that allows users to write blogs and ask questions.
Quick sum up: I have a social website where users can follow/post/vote etc. I
What am I doing wrong here? I have a .social div , but on
Ok, So, I have a social networking website where users can share a post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.