How can I order my entries by sum from a separate table?

Posted by bgadoci on Stack Overflow See other posts from Stack Overflow or by bgadoci
Published on 2010-05-12T06:23:31Z Indexed on 2010/05/12 6:24 UTC
Read the original article Hit count: 169

I am wondering how I can order posts in my PostController#index to display by a column total in a separate table. Here is how I have it set up.

class Post < ActiveRecord::Base
  :has_many :votes
end

and

Class Vote < ActiveRecord::Base
  :belongs_to :post
end

I user can either vote up or down a particular post. I know there are likely better ways to do what I am currently doing but looking for a fix given my current situation. When a user votes up a post, a value of 1 is passed to the Vote Table via a hidden field. When a user votes down a post a value of -1 is passed to the same column (names vote).

I am wondering how I can display my posts in order of the sum of the vote column (in the vote table) for a particular post.

Another way to say that is, if a particular post has a net vote sum of 5, I want that to appear above a post with a net vote sum of 4.

I am assuming that I need to affect the PostController#index action in some fashion. But not sure how to do that.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby