Stefan Gelenchev
Stefan Gelenchev

Reputation: 1

Order posts by number of votes

Hi everybody long time reader, first time poster :)

I have an app with posts and I am using the gem thumbs_up ( rails v. 3.0.7) to add votes to the posts. I have implemented a wall of fame for the posts showing the top 10 posts and I just can't figure out how to sort the posts by votes ( plusminus method).

Now I am just using:

def wall_of_fame
@posts = Post.tally(
  {   :at_least => 1,
      :at_most => 10000,
      :limit => 10,
      :order => 'vote_count desc'
  })

end

and for the wall of shame:

def wall_of_fame
@posts = Post.tally(
  {   :at_least => 1,
      :at_most => 10000,
      :limit => 10,
      :order => 'vote_count asc'
  })

end

but actually I need to order the posts using the plusminus method and not just vote_count, because it just shows me some posts, not the ones with the most up votes, or at least something like:

:order => 'votes_for asc'

and for the wall_of_shame

:order => 'votes_against asc'

Currently votes_for and votes_against can be used only in the views like

@post.votes_for

How can I use them in my posts controller to be able to use them in :order => ?

Thank you.

Upvotes: 0

Views: 935

Answers (2)

Teej
Teej

Reputation: 12873

You just need to order plusminus:

http://gistpages.com/2013/08/28/order_by_votes_using_the_thumbs_up_gem

Design.plusminus_tally.order('plusminus_tally DESC')

Upvotes: 1

thatdankent
thatdankent

Reputation: 950

I faced this issue as well, but I built my voting machinery from scratch. I ended up sorting my posts in the views. So, I redered them like this:

<%= render @posts.sort_by { |post| post.votes.size }.reverse %>

This ordered my posts from the most votes to the least votes.

Good Luck!

@thatdankent

Upvotes: 2

Related Questions