我正在寻找一种方法来对用户发布的主题进行排名。我现在正在使用这个:
Topic.select("topics.*, (((topics.likes_count - 1) / POW(((EXTRACT(EPOCH FROM (now()-topics.created_at)) / 3600)::integer + 2), 1.5))) AS popularity").order("popularity DESC")
我想知道如何将每个主题的评论数量添加到它。
主题.rb
has_many :comments, as: :commentable, dependent: :destroy
has_many :commenters, through: :comments
评论.rb
belongs_to :user
belongs_to :commentable, polymorphic: true, touch: true
belongs_to :commenter, class_name: 'User', foreign_key: :user_id
用户.rb
has_many :comments, :dependent => :destroy
has_many :topics #destroyed via the UserDeletionService