Ruby on rails 按评论数量对帖子进行排序,然后在Rails中按日期创建

Ruby on rails 按评论数量对帖子进行排序,然后在Rails中按日期创建,ruby-on-rails,sorting,Ruby On Rails,Sorting,我需要帮助弄清楚如何根据评论的数量对我的帖子进行排序。每当多篇文章有相同数量的评论时,它都会按最近发表的评论进行排序。我还试图弄清楚我应该在模型还是控制器中这样做 post.rb class Post < ActiveRecord::Base has_many :comments, :as => :commentable end class Comment < ActiveRecord::Base belongs_to :commentable, :polym

我需要帮助弄清楚如何根据评论的数量对我的帖子进行排序。每当多篇文章有相同数量的评论时,它都会按最近发表的评论进行排序。我还试图弄清楚我应该在模型还是控制器中这样做

post.rb

class Post < ActiveRecord::Base
    has_many :comments, :as => :commentable
end
class Comment < ActiveRecord::Base
    belongs_to :commentable, :polymorphic => true
    belongs_to :user
end
class PostsController < ApplicationController
    def index
        @feed = Post.find(:all, :order => "created_at ASC")
        @posts = Post.includes(:comments).order("comments.size ASC, created_at DESC").page(params[:page]).per(1)
   end
class Comment < ActiveRecord::Base
    belongs_to :commentable, :polymorphic => true, :counter_cache => true
    belongs_to :user
end
class PostsController < ApplicationController
    def index
        @posts = Post.order("comments_count, created_at DESC").page(params[:page]).per(1)
    end
end
class Post:可评论
结束
comment.rb

class Post < ActiveRecord::Base
    has_many :comments, :as => :commentable
end
class Comment < ActiveRecord::Base
    belongs_to :commentable, :polymorphic => true
    belongs_to :user
end
class PostsController < ApplicationController
    def index
        @feed = Post.find(:all, :order => "created_at ASC")
        @posts = Post.includes(:comments).order("comments.size ASC, created_at DESC").page(params[:page]).per(1)
   end
class Comment < ActiveRecord::Base
    belongs_to :commentable, :polymorphic => true, :counter_cache => true
    belongs_to :user
end
class PostsController < ApplicationController
    def index
        @posts = Post.order("comments_count, created_at DESC").page(params[:page]).per(1)
    end
end
class注释true
属于:用户
结束
posts\u controller.rb

class Post < ActiveRecord::Base
    has_many :comments, :as => :commentable
end
class Comment < ActiveRecord::Base
    belongs_to :commentable, :polymorphic => true
    belongs_to :user
end
class PostsController < ApplicationController
    def index
        @feed = Post.find(:all, :order => "created_at ASC")
        @posts = Post.includes(:comments).order("comments.size ASC, created_at DESC").page(params[:page]).per(1)
   end
class Comment < ActiveRecord::Base
    belongs_to :commentable, :polymorphic => true, :counter_cache => true
    belongs_to :user
end
class PostsController < ApplicationController
    def index
        @posts = Post.order("comments_count, created_at DESC").page(params[:page]).per(1)
    end
end
class PostsController“在ASC创建”)
@posts=Post.包括(:comments).order(“comments.size ASC,created_at DESC”).page(参数[:page])。每(1)
结束

我正在使用kaminari gem进行分页。我将提供任何有助于回答此问题的其他信息。

部分感谢Dave Newton为我提供了一个资源。我添加了计数器缓存以保持每个帖子的列总数的运行计数。到目前为止,这是有效的

comment.rb

class Post < ActiveRecord::Base
    has_many :comments, :as => :commentable
end
class Comment < ActiveRecord::Base
    belongs_to :commentable, :polymorphic => true
    belongs_to :user
end
class PostsController < ApplicationController
    def index
        @feed = Post.find(:all, :order => "created_at ASC")
        @posts = Post.includes(:comments).order("comments.size ASC, created_at DESC").page(params[:page]).per(1)
   end
class Comment < ActiveRecord::Base
    belongs_to :commentable, :polymorphic => true, :counter_cache => true
    belongs_to :user
end
class PostsController < ApplicationController
    def index
        @posts = Post.order("comments_count, created_at DESC").page(params[:page]).per(1)
    end
end
class注释true,:计数器\u缓存=>true
属于:用户
结束
posts\u controller.rb

class Post < ActiveRecord::Base
    has_many :comments, :as => :commentable
end
class Comment < ActiveRecord::Base
    belongs_to :commentable, :polymorphic => true
    belongs_to :user
end
class PostsController < ApplicationController
    def index
        @feed = Post.find(:all, :order => "created_at ASC")
        @posts = Post.includes(:comments).order("comments.size ASC, created_at DESC").page(params[:page]).per(1)
   end
class Comment < ActiveRecord::Base
    belongs_to :commentable, :polymorphic => true, :counter_cache => true
    belongs_to :user
end
class PostsController < ApplicationController
    def index
        @posts = Post.order("comments_count, created_at DESC").page(params[:page]).per(1)
    end
end
class PostsController
迁移

class AddCommentCounter < ActiveRecord::Migration
    def self.up
    add_column :posts, :comments_count, :integer, :null => false, :default => 0

    Post.reset_column_information
    Post.all.each do |p|
      p.update_attribute :comments_count, p.comments.length
    end
    end

    def self.down
    remove_column :posts, :comments_count
    end
end
class AddCommentCounterfalse、:default=>0
Post.reset\u列\u信息
Post.all.each do|p|
p、 更新属性:注释计数,p.comments.length
结束
结束
def自动关闭
删除列:帖子,:评论\u计数
结束
结束

到目前为止,我一直在按总评论数排序,然后按最近创建的评论数排序。这是railscasts链接:

当然不是控制器。如果你有一个计数器缓存,这将是微不足道的。你能详细说明吗?我不确定我是否理解你的第二句话。完成后,我该如何设置排序?