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 7400331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:14:02+00:00 2026-05-29T04:14:02+00:00

I added a counter cache called comments_count : class AddCommentsCountToMicroposts < ActiveRecord::Migration def change

  • 0

I added a counter cache called comments_count:

class AddCommentsCountToMicroposts < ActiveRecord::Migration
  def change
    add_column :microposts, :comments_count, :integer, :default => 0, :null => false
  end
end

to basically counts the number of comments for each micropost (Micropost model).
Now, this is how I order microposts by number of comments:

controllers/microposts_controller.rb:

  def index
    @microposts = Micropost.paginate(:page => params[:page],
                                     :per_page => 5).order('comments_count DESC')
  end

(I’m using the will_paginate gem)

Everything works as I want except that it is ordered created_at ASC. When there are two microposts with the same number of comments, the one created earlier is positioned first. I want the opposite, in other words, I want to order by comments_count DESC and created_at DESC at the same time.

Any suggestions to accomplish this?

  • 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-05-29T04:14:03+00:00Added an answer on May 29, 2026 at 4:14 am

    .order('comments_count DESC, created_at DESC')

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

Sidebar

Related Questions

I added a counter cache but can't get it to update. But I can
I added some custom fields (public booleans) to the global class in global.asax.cs which
I added a get_absolute_url function to one of my models. def get_absolute_url(self): return '/foo/bar'
I have a model object which did not have a counter cache on it
i want timer to run a counter from 0 to 3. i added the
I have a class that derives from enable_shared_from_this ... (Recently been added to std
The following is the example web server from the documentation, with an added counter.
I am adding elements dynamically using javascipt: $('.marker').append('<div class=container id=id' + counter + '
Added: Working with SQL Server 2000 and 2005, so has to work on both.
Added : I am conducting a study for a new system we're going to

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.