我添加了一个名为comments_count的计数器缓存
class AddCommentsCountToMicroposts < ActiveRecord::Migration
def change
add_column :microposts, :comments_count, :integer, :default => 0, :null => false
end
end基本上计算每个微博的评论数量(微博模式)。下面是我按评论数量排序微博的方式:
controllers/microposts_controller.rb:
def index
@microposts = Micropost.paginate(:page => params[:page],
:per_page => 5).order('comments_count DESC')
end(我正在使用will_paginate gem)
所有东西都按我的要求工作,除了它是按顺序排列的created_at ASC。当有两个具有相同数量的评论的微型帖子时,前面创建的一个将首先定位。我想要的正好相反,换句话说,我想同时订购comments_count DESC和created_at DESC。
有什么建议来做到这一点吗?
发布于 2012-01-31 20:20:34
.order('comments_count DESC, created_at DESC')
https://stackoverflow.com/questions/9086260
复制相似问题