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

  • Home
  • SEARCH
  • 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 8926263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:55:40+00:00 2026-06-15T07:55:40+00:00

I have a Post , Reply and a Vote model (polymorphic): create_table posts, :force

  • 0

I have a Post, Reply and a Vote model (polymorphic):

 create_table "posts", :force => true do |t|
    t.text     "content",       :limit => 255
    t.integer  "user_id"
    t.datetime "created_at",                                  :null => false
    t.datetime "updated_at",                                  :null => false
    t.string   "title"
    t.integer  "replies_count",                :default => 0, :null => false
    t.integer  "category_id"
  end

  create_table "replies", :force => true do |t|
    t.text     "content"
    t.integer  "post_id"
    t.integer  "user_id"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

  create_table "votes", :force => true do |t|
    t.integer  "votable_id"
    t.string   "votable_type"
    t.integer  "user_id"
    t.integer  "polarity"
    t.datetime "created_at",   :null => false
    t.datetime "updated_at",   :null => false
  end

I needed the total_votes of posts and replies so I created an instance method:

post.rb and reply.rb:

  def total_votes
    self.votes.map {|v| v.polarity }.sum
  end

So I can use it to sort posts and replies:

homepage:

 default_order = "created_at DESC"
 params[:order_by] ||= default_order
 @feed_items = @post.replies.paginate(page: params[:page],
                                      per_page: 10).order(params[:order_by])

So now, I’m not very sure what do add after order_by in the view:

  <span><%= link_to 'total_votes DESC', root_path(order_by: HERE) %></span>

I tried &:total_votes DESC and total_votes DESC but didn’t work.

What’s the right way of doing this? (I thought it was a bit of unnecessary to add a total_votes column to both posts and replies tables, but not sure if that is better for performance?)

  • 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-15T07:55:43+00:00Added an answer on June 15, 2026 at 7:55 am

    I would start with using the database to do the summing for you, but this is a little bit tricky because you need to order by a value that is calculated (an “aggregate”), but let’s set that aside.

    Your view will display items in the order determined when the instance variable is set in the controller. So in the controller, you may detect that there’s an order_by parameter (e.g. /feed?order_by=total_votes, and load the @feed_items accordingly, e.g.

    def index
      if params[:order_by] && params[:order_by] == 'total_votes'
        sort_order = "total_votes DESC"
      else
        sort_order = "created_at DESC"
      end
      @feed_items = @post.replies.paginate(page: params[:page],
                                      per_page: 10).order(sort_order)
    end
    

    then in your view, to change the sort order, create a link_to that adds the query string parameter (see the last example in the link_to api doc, e.g.

    <%= link_to "Sort by Total Votes", root_path(:order_by => 'total_votes') %><br />
    <%= link_to "Sort by Time", root_path %><br />
    

    Your other question is about how to do the sorting by votes. This answer may provide a good start for that, and an answer to your performance question: Rails: Order by sum of two columns

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

Sidebar

Related Questions

Hi I have a post model that :has_many :reply, when using searchlogic, doing Post.reply_content_like(search),
I have a text box where user can reply through it i should post
I am using cakePHP 1.26. In the PostsController, I have this: $this->paginate=array('conditions'=>array('Reply.post_id'=>$id),'limit' => 1);
I have a Post model object that has reference to a parent object. The
I have three models: User , Post and, Reply . An user has many
I'm working on a Ruby on Rails project where I have a post model
I have a many-to-many association between a Post and a Category model: categorization.rb: class
i have this problem to find a particular xml node l have post this
http://demo.thethemefoundry.com/traction/#post-183 Which wordpress plugin is this, to have post image on the left side
I have a post-build event that runs some commands for a c# project. The

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.