Ruby on rails 如何通过mongoid关联进行订购

Ruby on rails 如何通过mongoid关联进行订购,ruby-on-rails,ruby-on-rails-3,mongoid3,Ruby On Rails,Ruby On Rails 3,Mongoid3,我想通过一个集合和一个关联中的值进行排序 例如: 我得到了一个帖子的多重关联,比如: 评论 评级 附件 我如何通过以下协会订购帖子: 被大多数人评论的订单 按最受欢迎的客户订购 由大多数协会订购 谢谢。现在我可以回答这个问题了^^ 使用Mongoid,可以使用活动记录功能“计数器缓存”。 例如,我收到一篇带有引用评论的帖子: class Post include Mongoid::Document field :body, type: String has_many :co

我想通过一个集合和一个关联中的值进行排序

例如:

我得到了一个帖子的多重关联,比如:

  • 评论
  • 评级
  • 附件
我如何通过以下协会订购帖子:

  • 被大多数人评论的订单
  • 按最受欢迎的客户订购
  • 由大多数协会订购

谢谢。

现在我可以回答这个问题了^^

使用Mongoid,可以使用活动记录功能“计数器缓存”。 例如,我收到一篇带有引用评论的帖子:

class Post
  include Mongoid::Document
  field :body, type: String

  has_many :comments                                                                                                                                                                             
end



class Comment
  include Mongoid::Document
  field :body, type: String

  belongs_to :post, counter_cache: true                                                                                                                                                                             
end
在这种情况下,每个帖子实例都会得到一个comments\u count字段,其中包含帖子中引用的评论数

现在,您可以使用comments\u count字段对帖子进行排序。 请记住,此字段仅在至少存在一条注释时可用。
或者在模型中使用默认值显式设置comments_count字段

现在我可以回答这个问题了^^

使用Mongoid,可以使用活动记录功能“计数器缓存”。 例如,我收到一篇带有引用评论的帖子:

class Post
  include Mongoid::Document
  field :body, type: String

  has_many :comments                                                                                                                                                                             
end



class Comment
  include Mongoid::Document
  field :body, type: String

  belongs_to :post, counter_cache: true                                                                                                                                                                             
end
在这种情况下,每个帖子实例都会得到一个comments\u count字段,其中包含帖子中引用的评论数

现在,您可以使用comments\u count字段对帖子进行排序。 请记住,此字段仅在至少存在一条注释时可用。
或者在模型中使用默认值显式设置comments_count字段

我也很想知道,我也很想知道。