Last Updated: February 25, 2016
·
640
· dvilla

Use thumbs up and order your posts by the votes they have using a query

Yeah, a pretty long title, but very explicit. First of all, thumbs up is a ruby gem that enable to voting in your application, and allows entities (users) to vote on models. Knowing this, if you want to order your post by how many users have been voted on a post, in the index of your controller that pases the @variable you pass this:

def index
@posts = Post.select("posts.*, COUNT(voteableid) as TOTAL").joins("left join votes on votes.voteableid=posts.id").group("posts.id").order("total DESC")

end

It's better if you use a query instead of a sort_by it take less time